我不知道我的代码有什么问题,但每次我把小部件放在主屏幕上按钮1都不起作用。手机重启后,两个按钮都能正常工作......
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);
}
}
}
答案 0 :(得分:2)
您所做的是使用两种布局(appWidgetId
和views1
)更新相同的小部件(views2
)。基本上,您在button1
( - > views1
)上设置了具有待处理意图的小部件布局。
然后,您使用仅button2
具有待处理意图的布局覆盖它( - > views2
)。我不确定为什么这在重新启动后工作,也许安卓通知你使用相同的基本布局资源,并且不会在内部的某处创建它。
那你怎么做的呢?
您可以缩短您的代码段。创建一个布局,在同一布局上设置两个按钮的挂起意图,并像您一样进行更新。但是只有一次,你永远不需要每updateAppWidget()
次调用appWidgetId
(这与setContentView()基本上与普通活动相同)。
最终应该是这样的:
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);