无法从Widget打开活动

时间:2014-07-31 12:46:01

标签: android android-widget

我试图从小部件打开活动但是当我点击小部件按钮时无法打开活动,我的代码在下面给出

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        AppWidgetManager mgr = AppWidgetManager.getInstance(context);
        // retrieve a ref to the manager so we can pass a view update

        Intent i = new Intent();
        i.setClassName("com.avion.mychurch", "com.avion.mychurch.Welcome");
        PendingIntent myPI = PendingIntent.getService(context, 0, i, 0);
        // intent to start service

        // Get the layout for the App Widget
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.activity_main);

        // attach the click listener for the service start command intent
        views.setOnClickPendingIntent(R.id.wid_launch, myPI);

        // define the componenet for self
        ComponentName comp = new ComponentName(context.getPackageName(),
                MainActivity.class.getName());

        // tell the manager to update all instances of the toggle widget with
        // the click listener
        mgr.updateAppWidget(comp, views);

    }

1 个答案:

答案 0 :(得分:0)

您永远不会提供任何实际启动活动的代码。 添加此项并在单击时启动活动:

        Intent intent = new Intent(getApplicationContext(), YourActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        views(R.id.the_id_of_a_view_in_your_activity_main_layout, pendingIntent);