Android小部件的两个按钮,它们以不同的意图调用同一活动 [英] Two buttons of Android widgets calling same Activity with different intents

查看:40
本文介绍了Android小部件的两个按钮,它们以不同的意图调用同一活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中有一个带有两个按钮的homescreenwidget.这两个按钮都应该调用同一活动(类),只是使用不同的意图加上意图之外,才能知道哪个按钮称为类. 目前,只有button1在起作用并正在调用该活动.我还收到了称为活动"的键值.

I have a homescreenwidget in Android with two buttons. Both buttons should call the same activity ( class ) just use a different intent plus intent extras, to know which button called the class. For now only button1 is working and calling the activity. I also receive the keyvalue in the called Activity.

如何使第二个按钮起作用? 这是我的代码:

How can i make the second button work? Here's my code:

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

    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for ( int i =0; i<appWidgetIds.length ; i++){

        int appWidgetId = appWidgetIds[i];

        Intent intent2 = new Intent(context, Main.class);
        Intent intent1 = new Intent(context, Main.class);

        // Intent put extras Button 1
        String bread1 = "secure";
        Bundle basket1 = new Bundle();
        basket1.putString("key", bread1);
        intent1.putExtras(basket1);

        // Intent put extras Button 2
        String bread2 = "insecure";
        Bundle basket2 = new Bundle();
        basket2.putString("key", bread2);
        intent2.putExtras(basket2);

        PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0);
        PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0);

        RemoteViews views1 = new RemoteViews(context.getPackageName(),R.layout.maina);
        RemoteViews views2 = new RemoteViews(context.getPackageName(),R.layout.maina);

        views1.setOnClickPendingIntent(R.id.button1, pending1);
        views2.setOnClickPendingIntent(R.id.button2, pending2);

        appWidgetManager.updateAppWidget(appWidgetId, views1);
        appWidgetManager.updateAppWidget(appWidgetId, views2);

这是maina.xml

here is the maina.xml

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" android:weightSum="1"                android:orientation="vertical">
          <TextView android:layout_width="wrap_content"   android:layout_height="wrap_content" android:text="TextView" android:id="@+id/tvWidget"  android:textAppearance="?android:attr/textAppearanceLarge"></TextView>

           <LinearLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" android:weightSum="1"  android:orientation="horizontal">



           <Button android:text="@string/button1" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
            <Button android:text="@string/button2" android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>

          </LinearLayout>

</LinearLayout>

推荐答案

@Arnold,您创建了2个PendingIntent,它们是....

@Arnold you have created 2 PendingIntent which are....

PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0);
PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0);

其中PendingIntent.getActivity(Context context,int requestCode,Intent intent,int flags)有4个参数.您必须为不同的PendingIntent发送不同的" requestCode ".

Where PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags) has 4 parameters. You have to send different "requestCode" for different PendingIntents.

您的代码应该是....

Your code should be....

PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pending2 = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

在主类中,您需要创建此文件...

in the main class you need to create this...

String value;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

            Bundle b=intent.getExtras();


    try{
    value=b.getString("key");
    }
     catch (Exception e) {
        // TODO: handle exception
    }

    super.onReceive(context, intent);

}

带有onUpdate代码....

with the onUpdate code....

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

    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
            main.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    for (int widgetId : allWidgetIds) {
        // Create some random data

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.main);


        // Register an onClickListener for 1st button
        Intent intent = new Intent(context, main.class);

        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
        intent.putExtra("key", "1st Button");

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent);

        // Register an onClickListener for 2nd button............
        Intent intent2 = new Intent(context, main.class);

           intent2.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                   intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
           intent2.putExtra("key", "2nd Button");

        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,
                1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.button2, pendingIntent2);

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
}

然后,您可以检查 value = 1个第一个按钮还是 value = 2nd个按钮,以了解单击了哪个按钮. 它应该可以工作.如果它不能工作,请让我知道是什么问题...

Then you can check whether value=1st Button or value=2nd Button to know which Button has been clicked. It should work... IF it does not work please let me know what is the problem...

这篇关于Android小部件的两个按钮,它们以不同的意图调用同一活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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