【问题标题】:Android - How can we call some specific function when we click on app's home screen widget?Android - 当我们点击应用程序的主屏幕小部件时,我们如何调用某些特定功能?
【发布时间】:2011-04-21 17:37:55
【问题描述】:

我正在制作一个应用程序,它将有一个简单的主屏幕小部件(一个简单的图像视图)。

每当我在我的主屏幕上点击这个 imageview 小部件时,我想要调用一些特定的函数,如 abc() 或 xyz()。

我已经浏览了很多示例,但没有找到一个可以教我这一点的示例,并且 90% 的示例都使用了具有 setTextViewText 函数的 TextView,因此 TextView 一个很简单,但是当我们调用某些特定函数时点击imageview或者ImageButton如何设置onClick。

请提供帮助,如果您提供一些代码,我将不胜感激。

谢谢。

public class Sample extends Activity {

public static final String TAG = "Sample";
public void onCreate(Bundle saveInstanceState)
{
    Log.e(TAG, "I am HERE");
    Toast.makeText(this, "You Just Pressed Me", Toast.LENGTH_LONG).show();
}
}

这是我的更新方法-

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
    for(int i = 0; i < appWidgetIds.length; i++)
     {
         int appWidgetId = appWidgetIds[i];
         Intent intent = new Intent(context, Sample.class);
         //intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
         //intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.hswidget);
         views.setOnClickPendingIntent(R.id.widget, pendingIntent);

         appWidgetManager.updateAppWidget(appWidgetId, views);
     }
}

【问题讨论】:

    标签: android methods widget imageview homescreen


    【解决方案1】:

    Varundroid,

    我不认为您可以在点击 Widget 时启动特定功能,但您可以使用 PendingIntent 启动 Activity。这就是我目前的设置方式,所有Widget 包含的是ImageView。这是我的实现...

    XML:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <ImageView 
            android:layout_width="64dp" 
            android:layout_height="64dp"
            android:src="@drawable/icon" 
            android:id="@+id/widget_image" 
            android:scaleType="centerCrop"
            android:layout_centerInParent="true"/>
    
    </RelativeLayout>
    

    代码:

    public class WidgetProvider extends AppWidgetProvider {
    
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            final int N = appWidgetIds.length;
            for (int i=0; i<N; i++) {
                int appWidgetId = appWidgetIds[i];
    
                /* Create the PendingIntent for a QuickNote */
                Intent intent = new Intent(context, ItemEdit.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    
             // Get the layout for the App Widget and attach an on-click listener to the button
                RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_provider);
                views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
    
                // Tell the AppWidgetManager to perform an update on the current App Widget
                appWidgetManager.updateAppWidget(appWidgetId, views);
            }
        }
    }
    

    别忘了AndroidManifest.xml

        <receiver android:name=".WidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/appwidget_provider" />
        </receiver>
    

    【讨论】:

    • @willytate 我试过你的解决方案。我创建了一个新活动并将一条日志消息和 Toast 放入其中,以便我可以看到它是如何工作的,并且我在上面的问题中添加了此代码。但问题是它们(日志和吐司)都没有显示:-(请看看我的代码并告诉我我做错了什么。谢谢
    • @willytate 是的,我也这样做了。
    • @willy 每当我按下我的小部件时,它都会在 logcat 中向我显示这条消息:- 04-22 00:36:25.423: INFO/ActivityManager(59): 开始活动:Intent { flg=0x10000000 cmp =com.varundroid.util.completetaskmanager/.Sample bnds=[252,421][348,517] }.
    • 我没有看到您的代码有任何明显错误:[示例 Activity 也在清单中?
    • 致电finish() 或者您可以尝试使Activity 透明stackoverflow.com/questions/2176922/…
    【解决方案2】:

    我不明白你的问题是什么,但你上面提到的问题有非常简单的答案 喜欢 通过

    实例化imageview
    ImageView imgView=findViewById(R.id.imgView);
    

    然后将 onClickListner 设置为

    imgView.setOnClickListener(new view.OnClickListener(){
    
    public void onClick(View view){
    //call your functions
    }
    }
    );
    

    【讨论】:

    • 问题是关于主屏幕小部件和待处理的意图
    猜你喜欢
    • 1970-01-01
    • 2016-08-23
    • 2013-06-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多