开发者

One button on widget doesn't work

开发者 https://www.devze.com 2023-03-27 06:44 出处:网络
I don\'t know what is wrong in my code, but everytime i put widget on home screen button1 doesn\'t work. After phone reboot both buttons works perfectly...

I don't know what is wrong in my code, but everytime i put widget on home screen button1 doesn't work. After phone reboot both buttons works perfectly...

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.Remote开发者_C百科Views;



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

                }
    }

Any help, please ??

EDIT 2 : Ok, now it's working, but now I have another two questions . First i managed three buttons for sending messages, but I always get the same Message body, no mather what button I press. And second what do I need to change this code, so thah message is sent automaticly (now only composed message is opened) ....

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

                            }

                }

        }


What you do there is updating the same widget (appWidgetId) with two layouts (views1 and views2). Basically you are setting the widgets layout with a pending intent on button1 (->views1). Then you overwrite it with a layout where only button2 has a pending intent (-> views2). I'm not sure why this is working after a restart, maybe android notices that you use the same base layout resource and doesn't create it from scratch somewhere in the internals.

So how do you do it correct?

You may just shorten your snippet. Create one layout, set the pending intents for both buttons on the same layout and update as you did. But only once, you will never need more than one call to updateAppWidget() per appWidgetId (this is basically the same thing that setContentView() is for a normal activity).

Should look like this in the end:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消