2011-10-26 87 views
0

推出ActivityManager.RunningTaskInfo.topActivity我的onUpdate(...)的代碼MyAppWidgetProvider類的梅託德:無法從安卓的AppWidgetProvider

... 
Intent launchAppIntent = new Intent(context, SearchActivity.class);   
PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context, 0, launchAppIntent, 0);   
remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent); 
... 

它工作正常。新的SearchActivity通過點擊小部件開始。現在,我想開始點擊不是SearchActivity,而是從我的任務堆棧頂部開始的一項活動。爲此,我將以上代碼更改爲:

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);   
for (ActivityManager.RunningTaskInfo task:tasks) { 
    if (task.baseActivity.getShortClassName().equals(".MainActivity")) {  
     try { 
      Intent launchAppIntent = new Intent(context, Class.forName(task.topActivity.getClassName())); 
      PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context, 0, launchAppIntent, 0);   
      remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent);      
     } catch (ClassNotFoundException ex) { 
      Logger.getLogger(CashWidgetProvider.class.getName()).log(Level.SEVERE, null, ex); 
     }     
    } 
} 

不起作用。任務堆棧中的前臺活動不顯示在監視器上。而且,我在運行此代碼的日誌中沒有收到任何消息。

但是,相同的代碼可以很好地放置在繼承自Activity的類的某些方法中。

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); 
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);   
for (ActivityManager.RunningTaskInfo task:tasks) { 
    System.out.println(task.baseActivity.getShortClassName()); 
    if (task.baseActivity.getShortClassName().equals(".MainActivity")) { 
     try { 
      Intent launchAppIntent = new Intent(this, Class.forName(task.topActivity.getClassName())); 
      startActivity(launchAppIntent); 
     } catch (ClassNotFoundException ex) { 
      Logger.getLogger(AbstractActivity.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

我怎樣才能讓它爲AppWidget工作?

回答

1

這個問題似乎是由於小部件生命週期的誤解,像往常一樣...得到它的工作我要創建服務繼承覆蓋OnStart方法,代碼爲MyService類:

... 
Context context = this.getApplicationContext(); 
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);   
for (ActivityManager.RunningTaskInfo task:tasks) { 
    if (task.baseActivity.getShortClassName().equals(".MainActivity")) {  
     try { 
      Intent launchTopIntent = new Intent(context, Class.forName(task.topActivity.getClassName())); 
       context.startActivity(launchTopIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));    
     } catch (ClassNotFoundException ex) { 
      Logger.getLogger(CashWidgetProvider.class.getName()).log(Level.SEVERE, null, ex); 
     }     
    } 
} 
... 

和撥打以上爲MyService的AppWidgetProvider類的代碼來:

Intent launchAppIntent = new Intent(context, MyService.class); 
launchAppIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); 
PendingIntent launchAppPendingIntent = PendingIntent.getService(
      context.getApplicationContext(), 0, launchAppIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent2); 

不要忘了在AndroidManifest.xml下部分添加這個新的服務

<application> 
    ... 
    <service android:name=".MyService"/> 
</application> 

P.S.可能的話,最好使用BroadcastReceiver而不是Service。