Android 小部件的两个按钮以不同的意图调用相同的 Activity

2024-04-05

我在 Android 中有一个带有两个按钮的主屏幕小部件。两个按钮都应该调用相同的活动(类),只需使用不同的意图加上意图附加,即可知道哪个按钮调用了该类。 目前只有 Button1 正在工作并调用该活动。我还在被调用的活动中收到键值。

我怎样才能让第二个按钮起作用? 这是我的代码:

             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

          <?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,它们是......

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。

你的代码应该是......

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

在主类中你需要创建这个......

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 代码....

    @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);
    }
}

然后你可以检查是否值=第一个按钮 or 值=第二个按钮知道哪个按钮被点击。 它应该可以工作...如果它不起作用,请告诉我问题是什么...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 小部件的两个按钮以不同的意图调用相同的 Activity 的相关文章

随机推荐

  • 强制浏览器忽略缓存

    我的代码裁剪一张照片 裁剪后的照片会不断替换 但浏览器只会加载第一个裁剪的照片 我已经在网上搜索过 但没有任何效果 我已将随机字符串添加到 php new jpg time t 但这会阻止保存裁剪后的图像 我已经包括了 但浏览器仍然从缓存加
  • 根据因子使用不同的比例作为填充

    cnt 100 df lt data frame x c rnorm cnt mean 3 rnorm cnt mean 0 y rnorm 2 cnt g rep 0 1 each cnt ggplot df aes x y color
  • 多值 amChart 是否可以只显示一个 Y 轴?

    我有一个多值 AmChart 我在其中发送动态值并且按预期工作 但由于我动态发送百分比值的值 因此不需要为所有行单独使用 Y 轴 对于所有线来说 只有一个 y 轴就足够了 我可以禁用显示的 y 轴 并且还创建了单个 y 轴 静态 y 轴 但
  • Git 和二进制文件历史记录

    This is a follow up on some similar answered questions about git handling binary files https stackoverflow com questions
  • Flutter:如何根据设备的屏幕尺寸调整文本大小

    在 flutter 中 如何根据设备屏幕尺寸调整应用程序中的文本大小 该应用程序用于阅读文本 我需要用户随着设备尺寸的增加看到更大的文本 并设置最大可能的尺寸 我知道的一种方法就是这样做 Text Some text here style
  • 如何创建Windows服务来运行powershell脚本?

    我有无限循环 PowerShell 测试目的 脚本 我想将其作为 Windows Server 2008 R2 标准 中的服务运行 我使用以下命令来创建 Windows 服务 sc exe create My PS1Service binP
  • C 中是否有一个函数与 Python 中的 raw_input 功能相同?

    有没有一个C函数可以做同样的事情raw input在Python中 in Python x raw input Message Here 我怎样才能用C写出这样的东西 Update 我做了这个 但出现错误 include
  • Laravel 5.1:将数据传递给 View Composer

    我在 Laravel 5 1 中使用视图编辑器 很好 但是如何将参数传递给视图编辑器 就我而言 我将周信息 上周 当前 下周 包括日期 发送到我的视图中 并使用 de viewcomposer 当前星期是可变的 不仅来自 url 还来自控制
  • 在活动模式中使用 typeof<_>

    给出以下人为的活动模式 let TypeDef typeDef Type value obj if obj ReferenceEquals value null then None else let typ value GetType if
  • 编写更短的代码/算法是否更高效(性能)?

    在网站上看到代码高尔夫琐事后 很明显 人们试图找到在字符 行和总大小方面尽可能短的代码和算法的编写方法 即使这意味着编写如下内容 Code by job Topic Code Golf Collatz Conjecture n input
  • 如何构建 iPhone Xcode 项目?

    建立组 文件夹的好方法是什么 我已经尝试通过功能 功能加模型等的用户界面 与一个共同的组 我也尝试过UI 模型等 前者将类似的东西放在一起 这非常适合 iPhone 范式 后者意味着我会跳得更多 你怎么认为 标准的 Xcode MVC 文件
  • Chrome 中增加 5MB 存储限制

    我们有一个用 html5 编写的 POS 应用程序 我们使用 localStorage 来存储订单和其他信息 我遇到了 chrome 提供的 5MB 的限制 它导致应用程序崩溃 有没有简单的方法来增加这个限制 thanks 检查这个link
  • 何时断开连接以及何时结束 pg 客户端或池

    我的堆栈是node express 和pg 模块 我真的尝试通过文档和一些过时的教程来理解 我不知道何时以及如何断开和结束客户端 对于某些路线 我决定使用游泳池 这是我的代码 const pool new pg Pool user pool
  • Jaxb,类有两个同名属性

    使用 Jaxb jaxb impl 2 1 12 UI 尝试读取XML 文件 http www copypastecode com 75029 XML 文件中只有少数元素是有趣的 因此我想跳过大部分元素 我正在尝试读取的 XML
  • 当另一个下拉列表中的值发生变化时加载一个下拉列表的存储

    我有 2 个下拉菜单 根据在一个下拉列表中选择的值 我需要使用 JSON 进行 AJAX 调用来检索值并在其他下拉列表中可用 这需要在 EXTJS 中完成 我尝试了以下代码 FUNCTION NAME Field on select fun
  • powershell 2.0 命令行重定向

    我正在寻找以下差异的解释 给出以下 powershell 脚本foo ps1 write host normal write error error write host yay 运行它 C gt powershell foo ps1 gt
  • 在 tkinter 中的函数内调用函数

    打电话时rest来自按钮的功能 然后start函数被调用并每秒继续打印值但是当我再次调用时rest函数start再次调用函数 但这次启动函数以 2 倍速度打印值 依此类推 但我不想以 2 倍速度打印值 我正在做一个小项目 我遇到了这类问题
  • 为什么 Spring Data MongoDB 1.5.2 会因 NoSuchMethodError 失败?

    我似乎无法使用 spring mongodb 初始化最基本的 MongoTemplate 以下是我的 POM 中的相关摘录
  • 如何将 SqLite 与 BlackBerry OS 4.5 一起使用?

    我目前在 BlackBerry 中使用持久存储 我想在 BlackBerry OS 4 5 中使用 SqLite 数据库 但我找不到任何相关教程 我可以在 BlackBerry OS 4 5 中使用 SqLite 还是需要其他版本的 Bla
  • Android 小部件的两个按钮以不同的意图调用相同的 Activity

    我在 Android 中有一个带有两个按钮的主屏幕小部件 两个按钮都应该调用相同的活动 类 只需使用不同的意图加上意图附加 即可知道哪个按钮调用了该类 目前只有 Button1 正在工作并调用该活动 我还在被调用的活动中收到键值 我怎样才能