簡體   English   中英

Android Widget中未調用onReceive

[英]onReceive not getting called in Android Widget

當按下按鈕時,我無法獲得代碼以擊打小部件中的onReceive方法。 我很確定我清單中的所有設置正確且所有代碼正確。 我是否缺少某些東西,或者我必須添加一些東西來使它起作用?

表現

<?xml version="1.0" encoding="utf-8"?>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver android:name="Widget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="com.sense.widget.Widget.REFRESH" />
            <action android:name="com.sense.widget.Widget.PAGE_NEXT" />
            <action android:name="com.sense.widget.Widget.PAGE_PREV" /> 
        </intent-filter>

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

public class Widget extends AppWidgetProvider {
public static String REFRESH = "Refresh";
public static String PAGE_NEXT = "Next";
public static String PAGE_PREV = "Previous";
RemoteViews remoteViews;
ComponentName kWidget;

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

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);

    Intent intent = new Intent(context, getClass());
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, 0);

    intent.setAction(REFRESH);
    pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.refresh, pendingIntent);

    intent = new Intent(context, getClass());
    intent.setAction(PAGE_NEXT);
    pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.next, pendingIntent);

    intent = new Intent(context, getClass());
    intent.setAction(PAGE_PREV);
    pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.prev, pendingIntent);

    kWidget = new ComponentName(context, Widget.class);
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    appWidgetManager.updateAppWidget(kWidget, remoteViews);

}

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

    Toast.makeText(context, "here", Toast.LENGTH_LONG);
    super.onReceive(context, intent);
}

}

提供者

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="294dp"
android:updatePeriodMillis="100000"
android:initialLayout="@layout/main">

錯過了show(); 離開你的吐司:-)

Toast.makeText(context, "here", Toast.LENGTH_LONG).show();

問題出在我的PendingIntent's即時消息中,我使用的是getActivity而不是getBroadcast

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM