我不知道我的代碼中出了什麼問題,但每次我將小工具放在主屏幕上時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:好吧,現在它的工作,但現在我有另外兩個問題。首先,我管理了三個發送消息的按鈕,但我總是收到相同的消息正文,不管按下哪個按鈕。其次我需要做什麼來改變這個代碼,所以全自動發送thah消息(打開現在僅由消息)....
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);
}
}
}
您可以指定在作祟變量? – Phil