小部件上每个按钮的单独待定意图
Individual Pending Intent for each button on widget
首先抱歉我的英语不好
我有一个 AppWidget,它使用 Intent Service 来完成它的工作
我的应用程序小部件有 3 个按钮,如果用户按下每个按钮转到主 activity,但具有不同的 ContentViews,
现在的问题是任何时间用户单击任何按钮,它都会转到主 activity 但第一个 contentview ,我的 ID 始终是 1 ;
这是我的代码;
意向服务
public static class ToggleService extends IntentService{
public ToggleService(){
super("AppWidget$ToggleService");
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
ComponentName me = new ComponentName(this, AppWidget.class);
AppWidgetManager mgr = AppWidgetManager.getInstance(this);
mgr.updateAppWidget(me, buildUpdate(this));
}
public RemoteViews buildUpdate(Context ctx){
RemoteViews rmt = new RemoteViews(ctx.getPackageName(), R.layout.widget);
Intent i3 = new Intent(this,AppWidget.class);
i3.putExtra("ID", 3);
PendingIntent pi3 = PendingIntent.getBroadcast(ctx, 0, i3, 0);
Intent i1 = new Intent(this, AppWidget.class);
i1.putExtra("ID", 1);
PendingIntent pi1 = PendingIntent.getBroadcast(ctx, 0, i1, 0);
Intent i2 = new Intent(this,AppWidget.class);
i2.putExtra("ID", 2);
PendingIntent pi2 = PendingIntent.getBroadcast(ctx, 0, i2, 0);
rmt.setOnClickPendingIntent(R.id.widgetButton1, pi1);
rmt.setOnClickPendingIntent(R.id.widgetButton2, pi2);
rmt.setOnClickPendingIntent(R.id.widgetButton3, pi3);
return rmt;
}
}
OnRecive
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction()==null){
int ID_CODE=intent.getIntExtra("ID", 0);
Intent i = new Intent(context , MainActivity.class);
i.putExtra("ID", ID_CODE);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}else{
super.onReceive(context, intent);
}
更新时
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
context.startService(new Intent(context,ToggleService.class));
}
主要活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(android.os.Build.VERSION.SDK_INT>=11){
getActionBar().hide();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent i = getIntent();
if(i.getAction()!=null){
setContentView(R.layout.launcher);
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
}
}, 2000);}
else {
int ID=i.getIntExtra("ID", 0);
if(ID==1){
setContentView(R.layout.activity_main);
Toast.makeText(this, String.valueOf(ID), Toast.LENGTH_LONG).show();
}
if(ID==2){
setContentView(R.layout.bank_main);
Toast.makeText(this, "2", Toast.LENGTH_LONG).show();
}
if(ID==3){
setContentView(R.layout.other_main);
Toast.makeText(this, "3", Toast.LENGTH_LONG).show();
}
}
}
问题是每次我点击每个 widgetButton 时,ID 的值始终为 1,并且永远不会更改为 2 或 3
请帮帮我
当您创建 PendingIntent
时,您就是在一遍又一遍地重复使用同一个。您希望同时激活 3 PendingIntent
s。为此,您需要确保调用 PendingIntent.getBroadcast()
的参数确保您将获得唯一的 PendingIntent
。最简单的方法是使用不同的唯一 requestCode
参数。像这样:
Intent i3 = new Intent(this,AppWidget.class);
i3.putExtra("ID", 3);
PendingIntent pi3 = PendingIntent.getBroadcast(ctx, 3, i3, 0);
Intent i1 = new Intent(this, AppWidget.class);
i1.putExtra("ID", 1);
PendingIntent pi1 = PendingIntent.getBroadcast(ctx, 1, i1, 0);
Intent i2 = new Intent(this,AppWidget.class);
i2.putExtra("ID", 2);
PendingIntent pi2 = PendingIntent.getBroadcast(ctx, 2, i2, 0);
注意:只是 "extras" 不同并不能使 Intent
参数与 PendingIntent.getBroadcast()
唯一。您需要使用不同的 Intent
操作或不同的 requestCode
参数。
首先抱歉我的英语不好
我有一个 AppWidget,它使用 Intent Service 来完成它的工作 我的应用程序小部件有 3 个按钮,如果用户按下每个按钮转到主 activity,但具有不同的 ContentViews, 现在的问题是任何时间用户单击任何按钮,它都会转到主 activity 但第一个 contentview ,我的 ID 始终是 1 ; 这是我的代码; 意向服务
public static class ToggleService extends IntentService{
public ToggleService(){
super("AppWidget$ToggleService");
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
ComponentName me = new ComponentName(this, AppWidget.class);
AppWidgetManager mgr = AppWidgetManager.getInstance(this);
mgr.updateAppWidget(me, buildUpdate(this));
}
public RemoteViews buildUpdate(Context ctx){
RemoteViews rmt = new RemoteViews(ctx.getPackageName(), R.layout.widget);
Intent i3 = new Intent(this,AppWidget.class);
i3.putExtra("ID", 3);
PendingIntent pi3 = PendingIntent.getBroadcast(ctx, 0, i3, 0);
Intent i1 = new Intent(this, AppWidget.class);
i1.putExtra("ID", 1);
PendingIntent pi1 = PendingIntent.getBroadcast(ctx, 0, i1, 0);
Intent i2 = new Intent(this,AppWidget.class);
i2.putExtra("ID", 2);
PendingIntent pi2 = PendingIntent.getBroadcast(ctx, 0, i2, 0);
rmt.setOnClickPendingIntent(R.id.widgetButton1, pi1);
rmt.setOnClickPendingIntent(R.id.widgetButton2, pi2);
rmt.setOnClickPendingIntent(R.id.widgetButton3, pi3);
return rmt;
}
}
OnRecive
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction()==null){
int ID_CODE=intent.getIntExtra("ID", 0);
Intent i = new Intent(context , MainActivity.class);
i.putExtra("ID", ID_CODE);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}else{
super.onReceive(context, intent);
}
更新时
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
context.startService(new Intent(context,ToggleService.class));
}
主要活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(android.os.Build.VERSION.SDK_INT>=11){
getActionBar().hide();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent i = getIntent();
if(i.getAction()!=null){
setContentView(R.layout.launcher);
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
}
}, 2000);}
else {
int ID=i.getIntExtra("ID", 0);
if(ID==1){
setContentView(R.layout.activity_main);
Toast.makeText(this, String.valueOf(ID), Toast.LENGTH_LONG).show();
}
if(ID==2){
setContentView(R.layout.bank_main);
Toast.makeText(this, "2", Toast.LENGTH_LONG).show();
}
if(ID==3){
setContentView(R.layout.other_main);
Toast.makeText(this, "3", Toast.LENGTH_LONG).show();
}
}
}
问题是每次我点击每个 widgetButton 时,ID 的值始终为 1,并且永远不会更改为 2 或 3 请帮帮我
当您创建 PendingIntent
时,您就是在一遍又一遍地重复使用同一个。您希望同时激活 3 PendingIntent
s。为此,您需要确保调用 PendingIntent.getBroadcast()
的参数确保您将获得唯一的 PendingIntent
。最简单的方法是使用不同的唯一 requestCode
参数。像这样:
Intent i3 = new Intent(this,AppWidget.class);
i3.putExtra("ID", 3);
PendingIntent pi3 = PendingIntent.getBroadcast(ctx, 3, i3, 0);
Intent i1 = new Intent(this, AppWidget.class);
i1.putExtra("ID", 1);
PendingIntent pi1 = PendingIntent.getBroadcast(ctx, 1, i1, 0);
Intent i2 = new Intent(this,AppWidget.class);
i2.putExtra("ID", 2);
PendingIntent pi2 = PendingIntent.getBroadcast(ctx, 2, i2, 0);
注意:只是 "extras" 不同并不能使 Intent
参数与 PendingIntent.getBroadcast()
唯一。您需要使用不同的 Intent
操作或不同的 requestCode
参数。