小部件上每个按钮的单独待处理意图

时间:2015-02-07 11:36:58

标签: android android-intent widget android-widget

首先抱歉我的英语不好

我有一个AppWidget,它使用Intent Service来完成它的工作 我的应用程序小部件有3个按钮,如果用户按下每个按钮转到主要活动,但使用不同的ContentViews, 现在问题是任何时间用户点击任何按钮,它会进入主要活动但是有第一个内容视图,我的ID总是1; 这是我的代码; 意向服务

public static class ToggleService extends IntentService{
    public ToggleService(){
        super("AppWidget$ToggleService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        ComponentName me = new ComponentName(this, AppWidget.class);
        AppWidgetManager mgr = AppWidgetManager.getInstance(this);
        mgr.updateAppWidget(me, buildUpdate(this));




    }
    public RemoteViews buildUpdate(Context ctx){
        RemoteViews rmt = new RemoteViews(ctx.getPackageName(), R.layout.widget);
        Intent i3 = new Intent(this,AppWidget.class);
        i3.putExtra("ID", 3);
        PendingIntent pi3 = PendingIntent.getBroadcast(ctx, 0, i3, 0);
        Intent i1 = new Intent(this, AppWidget.class);
        i1.putExtra("ID", 1);
        PendingIntent pi1 = PendingIntent.getBroadcast(ctx, 0, i1, 0);
        Intent i2 = new Intent(this,AppWidget.class);
        i2.putExtra("ID", 2);
        PendingIntent pi2 = PendingIntent.getBroadcast(ctx, 0, i2, 0);




        rmt.setOnClickPendingIntent(R.id.widgetButton1, pi1);
        rmt.setOnClickPendingIntent(R.id.widgetButton2, pi2);
        rmt.setOnClickPendingIntent(R.id.widgetButton3, pi3);



        return rmt;

    }
}

OnRecive

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if(intent.getAction()==null){
        int ID_CODE=intent.getIntExtra("ID", 0);
        Intent i = new Intent(context , MainActivity.class);
        i.putExtra("ID", ID_CODE);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);



    }else{
    super.onReceive(context, intent);
}

OnUpdate

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub

    context.startService(new Intent(context,ToggleService.class));
}

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(android.os.Build.VERSION.SDK_INT>=11){
        getActionBar().hide();

    }
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    Intent i = getIntent();
    if(i.getAction()!=null){        

    setContentView(R.layout.launcher);

    Handler mHandler = new Handler();
    mHandler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            setContentView(R.layout.activity_main);
        }
    }, 2000);}
    else {
        int ID=i.getIntExtra("ID", 0);
        if(ID==1){
            setContentView(R.layout.activity_main);
            Toast.makeText(this, String.valueOf(ID), Toast.LENGTH_LONG).show();
        }
        if(ID==2){

            setContentView(R.layout.bank_main);
            Toast.makeText(this, "2", Toast.LENGTH_LONG).show();
        }
        if(ID==3){
            setContentView(R.layout.other_main);
            Toast.makeText(this, "3", Toast.LENGTH_LONG).show();
        }
    }
    }

问题是每次我点击每个widgetButton,ID的值总是1,它永远不会变为2或3 请帮帮我

1 个答案:

答案 0 :(得分:1)

当您创建PendingIntent时,您将一遍又一遍地重复使用同一个。您希望同时激活3 PendingIntent个。为此,您需要确保调用PendingIntent.getBroadcast()的参数确保您获得唯一的PendingIntent。最简单的方法是使用不同的唯一requestCode参数。像这样:

    Intent i3 = new Intent(this,AppWidget.class);
    i3.putExtra("ID", 3);
    PendingIntent pi3 = PendingIntent.getBroadcast(ctx, 3, i3, 0);
    Intent i1 = new Intent(this, AppWidget.class);
    i1.putExtra("ID", 1);
    PendingIntent pi1 = PendingIntent.getBroadcast(ctx, 1, i1, 0);
    Intent i2 = new Intent(this,AppWidget.class);
    i2.putExtra("ID", 2);
    PendingIntent pi2 = PendingIntent.getBroadcast(ctx, 2, i2, 0);

注意:只是有不同的"额外"不会将Intent参数设为PendingIntent.getBroadcast()唯一。您需要使用不同的Intent操作或不同的requestCode参数。