gpt4 book ai didi

android - 小部件上的一个按钮不起作用

转载 作者:行者123 更新时间:2023-11-29 00:44:41 25 4
gpt4 key购买 nike

我不知道我的代码有什么问题,但每次我将小部件放在主屏幕上时,button1 都不起作用。手机重启后,两个按钮都可以正常工作...

package cro.perger.bonbon;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;



public class HelloWidget extends AppWidgetProvider {

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

String encodedHash = Uri.encode("#");

for (int appWidgetId : appWidgetIds) {

Intent callIntent1 = new Intent("android.intent.action.CALL",
Uri.parse("tel:*100" + encodedHash));
Intent callIntent2 = new Intent("android.intent.action.CALL",
Uri.parse("tel:*200*1" + encodedHash));

PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, callIntent1, 0);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, callIntent2, 0);

RemoteViews views1 = new RemoteViews(context.getPackageName(), R.layout.widget);
views1.setOnClickPendingIntent(R.id.button1, pendingIntent1);

RemoteViews views2 = new RemoteViews(context.getPackageName(), R.layout.widget);
views2.setOnClickPendingIntent(R.id.button2, pendingIntent2);

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

}
}

有什么帮助吗??

编辑 2:好的,现在可以使用了,但现在我还有另外两个问题。首先,我管理了三个用于发送消息的按钮,但无论我按什么按钮,我总是得到相同的消息正文。其次,我需要更改此代码,以便自动发送消息(现在只打开组合消息)....

package cro.perger.bonbon;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;



public class HelloWidget extends AppWidgetProvider {

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

String encodedHash = Uri.encode("#");

for (int appWidgetId : appWidgetIds) {

Intent callIntent1 = new Intent("android.intent.action.CALL",
Uri.parse("tel:*100" + encodedHash));
Intent callIntent2 = new Intent("android.intent.action.CALL",
Uri.parse("tel:*200*1" + encodedHash));

Intent sendIntent1 = new Intent(Intent.ACTION_VIEW);
sendIntent1.putExtra("sms_body", "Poruka 1");
sendIntent1.putExtra("address", "5556");
sendIntent1.setType("vnd.android-dir/mms-sms");

Intent sendIntent2 = new Intent(Intent.ACTION_VIEW);
sendIntent2.putExtra("sms_body", "Poruka 2");
sendIntent2.putExtra("address", "5556");
sendIntent2.setType("vnd.android-dir/mms-sms");

Intent sendIntent3 = new Intent(Intent.ACTION_VIEW);
sendIntent3.putExtra("sms_body", "Poruka 3");
sendIntent3.putExtra("address", "5556");
sendIntent3.setType("vnd.android-dir/mms-sms");

Intent openintent = new Intent(context, bonbon.class);

PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, callIntent1, 0);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, callIntent2, 0);
PendingIntent pendingIntent3 = PendingIntent.getActivity(context, 0, sendIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent4 = PendingIntent.getActivity(context, 0, sendIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent5 = PendingIntent.getActivity(context, 0, sendIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent6 = PendingIntent.getActivity(context, 0, openintent, 0);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.button1, pendingIntent1);
views.setOnClickPendingIntent(R.id.button2, pendingIntent2);
views.setOnClickPendingIntent(R.id.button3, pendingIntent3);
views.setOnClickPendingIntent(R.id.button4, pendingIntent4);
views.setOnClickPendingIntent(R.id.button5, pendingIntent5);
views.setOnClickPendingIntent(R.id.button6, pendingIntent6);

appWidgetManager.updateAppWidget(appWidgetId, views);

}

}

}

最佳答案

您所做的是用两种布局(views1views2)更新同一个小部件 (appWidgetId)。基本上,您是在 button1 (->views1) 上使用未决 Intent 设置小部件布局。然后用只有 button2 有未决 Intent (-> views2) 的布局覆盖它。我不确定为什么这在重新启动后会起作用,也许 android 注意到您使用相同的基本布局资源并且不会在内部某处从头开始创建它。

那么你如何正确地做到这一点?

您可以缩短您的代码段。创建一个布局,为同一布局上的两个按钮设置待定 Intent 并像您所做的那样进行更新。但是只有一次,您永远不需要对每个 appWidgetId 多次调用 updateAppWidget()(这与 setContentView() 用于正常 Activity 的基本相同)。

最后应该是这样的:

Intent callIntent1  = new Intent("android.intent.action.CALL",
Uri.parse("tel:*100" + encodedHash));
Intent callIntent2 = new Intent("android.intent.action.CALL",
Uri.parse("tel:*200*1" + encodedHash));

PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, callIntent1, 0);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, callIntent2, 0);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.button1, pendingIntent1);
views.setOnClickPendingIntent(R.id.button2, pendingIntent2);

appWidgetManager.updateAppWidget(appWidgetId, views);

关于android - 小部件上的一个按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7033503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com