尝试从AppWidgetProvider打开一个新活动

时间:2011-10-20 03:03:47

标签: android

public class MyWidgetProvider extends AppWidgetProvider {
.....
}

public void onEnabled (Context contex){
    Intent myIntent = new Intent(context, ReadName.class);
    context.startActivity(myIntent);
}

好吧,我尝试了system.out.println(“”)它的工作原理,意味着它在LogCat上显示,但是可以从这个方法onEnabled(Context contex)打开一个新的活动谢谢


我正在尝试这种方式,但它没有显示:我是一个新的活动onload小部件,

public void onEnabled (Context contex){
    System.out.println("new task flag");
    Intent i = new Intent(contex, ReadName.class);

    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    contex.startActivity(i);
}

2 个答案:

答案 0 :(得分:0)

它应该工作。还可以通过将标志设置为FLAG_ACTIVITY_NEW_TASK来尝试。

此文档的链接是here

答案 1 :(得分:0)

这是AppWidgetProvider的overoverder方法,ClickOneActivity是我想要移动的活动... 检查一下它对你有用的代码......

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        Intent configIntent = new Intent(context, ClickOneActivity.class);
        configIntent.setAction(ACTION_WIDGET_CONFIGURE);

        Intent active = new Intent(context, ButtonWidget.class);
        active.setAction(ACTION_WIDGET_RECEIVER);
        active.putExtra("msg", "Message for Button 1");
        //when you will click button1 the message "Message for Button 1" will appear as a notification
//you can do whatever you want anyway on the press of this button
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

        remoteViews.setOnClickPendingIntent(R.id.button_one, actionPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.button_two, configPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }

所有最好的