2016-07-31 47 views
0

我編寫了一個帶有兩個按鈕的Android小部件。如果我開始「活動」一切正常,我可以點擊這些按鈕。但是,如果我鎖定手機,過程幾秒鐘應用程序進程死後無法點擊Android小部件

​​

後死了,我再次嘗試點擊這些按鈕,沒有發生,除了LOGCAT

I/ActivityManager: filter receiver for action = ButtonRefresh


updateAppWidget,稱爲通過@Override onUpdate(...)

private final static String BUTTON_REFRESH = "ButtonRefresh"; 

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

    final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myclasslayout); 

    //Click listener depending on @this (getPendingSelfIntent) 
    views.setOnClickPendingIntent(R.id.button_refresh, getPendingSelfIntent(context, BUTTON_REFRESH)); 

    [...] 

    appWidgetManager.updateAppWidget(appWidgetId, views); 
} 


getPendingSelfIntent()

private static PendingIntent getPendingSelfIntent(Context context, String action) { 
    Intent intent = new Intent(context, MyClass.class); 
    intent.setAction(action); 
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 


onReceive()

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

    if (BUTTON_REFRESH.equals(intent.getAction())) { 
     Log.i("MyLogtag", "Refresh"); //This never shows up in LOGCAT 
    } 
} 


AndroidManifest.xml

[...] 
<receiver android:name=".MyClass"> 
    <intent-filter> 
     <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
    </intent-filter> 
    <meta-data 
     android:name="android.appwidget.provider" 
     android:resource="@xml/myclass_info" /> 
</receiver> 
[...] 

的onReceive 當應用程序是dead從未被調用......

回答

0

我知道了......

我不得不改變

private final static String BUTTON_REFRESH = "ButtonRefresh"; 

private final static String BUTTON_REFRESH = "com.example.mypackage.BUTTON_REFRESH"; 

AndroidManifest

<receiver android:name=".MyClass"> 
<intent-filter> 
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
    <action android:name="com.example.mypackage.BUTTON_REFRESH"/> 
</intent-filter> 
<meta-data 
    android:name="android.appwidget.provider" 
    android:resource="@xml/myclass_info" /> 
</receiver> 
相關問題