从Android小工具上单击开始意图 [英] Start intent from Android Widget on click

查看:137
本文介绍了从Android小工具上单击开始意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以更新一个部件,但我有开始从部件点击一个新的意图一些麻烦。

i can update a widget but i have some troubles starting a new intent on click from the widget.

这是code:

        try { 
        remoteViews.setOnClickPendingIntent(R.id.widgetIcon,
                    openGooglePlay(context, bundle));

        } catch (NullPointerException e) {
            Log.d(TAG,
                    "Error loading google play from widget: " + e.getMessage());
        }

我很困惑,为什么这个意图是不是在点击启动。应该连接到资源 widgetIcon 只所以不与任何其他相混淆,对吧?

I am confused why this intent is not started on click. It should be attached to resource widgetIcon only and so not be confused with anything else, right?

推荐答案

确保AppWidgetprovider的onUpdate方法被调用,您的设置挂起的意图的权利。试着调试,看看是否中的onUpdate叫当您在主屏幕中添加小部件。

Make sure the AppWidgetprovider onUpdate method is called, and your setting the right pending intent. Try to debug and see if onUpdate in called when you add the widget in home screen.

下面是一个示例工作code ..

Here is a sample working code..

public class CustomAppWidgetProvider 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];

        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
        views.setOnClickPendingIntent(R.id.button1, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
 }
}

这篇关于从Android小工具上单击开始意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆