我的应用程序小部件的Remoteview接收器随机接听电话

时间:2018-05-29 09:12:29

标签: android broadcastreceiver android-widget android-pendingintent appwidgetprovider

我是初学者,正在使用app小部件。在这里,我已经制作了一个应用程序的应用程序小部件,并在其类设置接收器点击小部件icon.Its工作,当用户点击它。但问题是接收器有时会被自动调用。 我不明白为什么会这样。希望帮助。

我的应用小部件的课程:

MyWidget.java:

public class MyWidget extends AppWidgetProvider {

    static Context cont;
    static SharedPreferences preferences;


    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
        cont = context;
        Intent intent2 = new Intent(context, MyReceiver.class);
        PendingIntent pendingIntent = PendingIntent.
                getBroadcast(context, 0,
                        intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
        views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }


    @Override
    public void onEnabled(Context context) {
        preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
        preferences.edit().putBoolean("key", true).commit();
    }

    @Override
    public void onDisabled(Context context) {
        preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
        preferences.edit().putBoolean("key", false).commit();

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

    }
}

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="*************************">


    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:roundIcon="@drawable/logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Home">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".Myservice" />
      <receiver android:name=".MyReceiver"></receiver>
        <receiver android:name=".MyWidget">
            <intent-filter>
                <action android:name="**********************" />
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/my_widget_info" />
        </receiver>
    </application>

</manifest>

0 个答案:

没有答案