点击小部件时隐藏主要活动

时间:2011-12-02 21:14:53

标签: android button android-widget

我已经为Android开发了一个app小部件,当点击时,会出现带有Dialog Theme的活动。但是,如果从主活动按下主页按钮,然后尝试再次点击小部件,则主要活动将显示在背景上。有没有办法防止这种情况发生?即当小部件被轻拍时不显示的主要活动

的AndroidManifest.xml

<receiver android:name=".myWidget" >
<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
           android:resource="@xml/res_widget" />
</receiver>

AppWidgetProvider.java

public void onUpdate(Context c, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
    final int N = appWidgetIds.length;
     for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
    RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
    Intent fireDialog = new Intent(c,Execute.class);
    fireDialog.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // if i click home when i am in main activity i still get it on background-this doesnt help neither though
    PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, fireDialog, 0);
    views.setOnClickPendingIntent(R.id.myButton, pendingIntent);
    Log.w(LOG_TAG,"Called");
    appWidgetManager.updateAppWidget(appWidgetId, views);
     }
}

public void onReceive(Context c, Intent intent) 
{
 final String action=intent.getAction();
 if(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)){
     final int appWidgetId=intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
     if(appWidgetId!=AppWidgetManager.INVALID_APPWIDGET_ID){
         this.onDeleted(c,new int[]{appWidgetId});
     }
 }else{
     super.onReceive(c, intent);
 }

0 个答案:

没有答案