永远不会调用Android AppWidgetProvider的onReceive方法

编程入门 行业动态 更新时间:2024-10-21 10:03:12
本文介绍了永远不会调用Android AppWidgetProvider的onReceive方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个Android小部件.这是我的AppWidgetProvider类:

I'm working on an android widget. This is my AppWidgetProvider class:

public class MyWidgetProvider extends AppWidgetProvider { public static String ACTION_CHARGE = "Charge"; public static String ACTION_BALANCE = "Balance"; public static String ACTION_SEND_BALANCE = "SendBalance"; public static String ACTION_CALL_ME = "CallMe"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Get all ids ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class); int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); for (int widgetId : allWidgetIds) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); // Register onClickListeners Intent intent = new Intent(context, MyWidgetProvider.class); intent.setAction(ACTION_CHARGE); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.ib_charge, pendingIntent); intent = new Intent(context, MyWidgetProvider.class); intent.setAction(ACTION_BALANCE); pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.ib_balance, pendingIntent); intent = new Intent(context, MyWidgetProvider.class); intent.setAction(ACTION_SEND_BALANCE); pendingIntent = PendingIntent.getBroadcast(context, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.ib_charge_send, pendingIntent); intent = new Intent(context, MyWidgetProvider.class); intent.setAction(ACTION_CALL_ME); pendingIntent = PendingIntent.getBroadcast(context, 3, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.ib_call_me, pendingIntent); appWidgetManager.updateAppWidget(widgetId, remoteViews); } } @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_CHARGE)) { Log.d("onReceive", ACTION_CHARGE); context.startActivity(new Intent("android.intent.action.CALL", Uri .parse("tel:*789*1415" + Uri.encode("#")))); } else if (intent.getAction().equals(ACTION_BALANCE)) { Log.d("onReceive", ACTION_BALANCE); } else if (intent.getAction().equals(ACTION_SEND_BALANCE)) { Log.d("onReceive", ACTION_SEND_BALANCE); } else if (intent.getAction().equals(ACTION_CALL_ME)) { Log.d("onReceive", ACTION_CALL_ME); } super.onReceive(context, intent); Log.d("onReceive", "***********************************************************************"); } }

ٌٌ但是onReceive方法永远不会被调用.哪里出问题了?

ٌٌBut the onReceive method never gets called. Where's the problem?

推荐答案

如果您在提供程序中使用如下配置活动,请记住在配置活动中使用setOnClickPendingIntent(),否则将不会使用onReceive()方法以后再打电话,即使其他一切都正确.

If you are using a configure activity in you provider as below, please remember to setOnClickPendingIntent() in you configure activity, or onReceive() method won't be called later, even if the other things are all correct.

<appwidget-provider xmlns:android="schemas.android/apk/res/android" android:minWidth="40dp" android:minHeight="40dp" android:updatePeriodMillis="86400000" android:previewImage="@drawable/icon" android:initialLayout="@layout/appwidget" android:configure="AppWidgetConfigure" android:resizeMode="horizontal|vertical" android:widgetCategory="home_screen"> </appwidget-provider>

更多推荐

永远不会调用Android AppWidgetProvider的onReceive方法

本文发布于:2023-11-26 20:57:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   方法   AppWidgetProvider   Android   onReceive

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!