2012-03-12 54 views
1

我有這個小部件,切換聲亮滅,而是那我想打電話給另一個活動(MYxz.class)請告訴我,我應該怎麼改這裏...如何在點擊我的小工具時啓動活動?

public class AppWidget extends AppWidgetProvider { 

@Override 
public void onReceive(Context ctxt, Intent intent) 
{ 
     if(intent.getAction()==null) 
     { 
      ctxt.startService(new Intent(ctxt,ToggleService.class)); 
     } 
    else 
     { 
    super.onReceive(ctxt, intent); 
} 

} 

    @Override 
    public void onUpdate(Context context,AppWidgetManager appWidgetManager, int [] appWidgetIds) 
    { 
context.startService(new Intent(context,ToggleService.class)); 
//RemoteViews buildUpdate(context); 
    } 

    public static class ToggleService extends IntentService 
    { 
public ToggleService() { 
    super("AppWidget$ToggleService"); 

    } 



    @Override 
    protected void onHandleIntent(Intent intent) 
{ 
    ComponentName me = new ComponentName(this,AppWidget.class); 
    AppWidgetManager mgr= AppWidgetManager.getInstance(this); 
    mgr.updateAppWidget(me,buildUpdate(this)); 
} 

private RemoteViews buildUpdate(Context context) 
{ 
RemoteViews updateViews=new RemoteViews(context.getPackageName(),R.layout.widget); 
AudioManager audioManager=(AudioManager)context.getSystemService(Activity.AUDIO_SERVICE); 
if(audioManager.getRingerMode()==AudioManager.RINGER_MODE_SILENT) 
{ 
    updateViews.setImageViewResource(R.id.phoneState,R.drawable.silent); 
    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 

} 
else { 
    updateViews.setImageViewResource(R.id.phoneState,R.drawable.phone123); 
    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); 

} 
Intent i=new Intent(this, AppWidget.class); 
PendingIntent pi= PendingIntent.getBroadcast(context,0, i,0); 
updateViews.setOnClickPendingIntent(R.id.phoneState,pi); 
return updateViews; 
} 

} 
} 

回答

1

相反的:

Intent active = new Intent(paramContext, AppWidget.class); 

您使用:

Intent active = new Intent(paramContext, YOURCLASS.class); 

創建掛起的意圖之前,@imran汗幫了我,但也有一些調整,你應該做的2 ...這應該火起來的Activity需要。

+1

非常感謝:Tony – 2012-03-13 10:10:22

1

隨着使用pendingIntents你可以調用一個意圖(對你的活動或其他)當一個小工具項目點擊。這可能幫助: http://www.vogella.de/articles/AndroidWidgets/article.html

+0

,這是一個原生Android樣本: http://developer.android.com/resources/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetProvider.html – 2012-03-12 17:33:53

2

是可以看到例如:

UPADTE您的manifest.xml

<receiver android:name=".AppWidget" 
     android:label="Caller" 
     android:icon="@drawable/ic_launcher" > 
     <intent-filter > 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
<action android:name="com.app.example.MyWidget.ACTION_WIDGET_CLICK_RECEIVER"/> 

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

    </receiver> 
    <service android:name=".AppWidget$ToggleService" /> 

和更新你的AppWidgetProvider

 public class MyWidget extends AppWidgetProvider { 
        public static String ACTION_WIDGET_CLICK_RECEIVER = "ActionReceiverWidget"; 

        public static int appid[]; 
        public static RemoteViews rview; 
        @Override 
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
          int[] appWidgetIds){ 
         updateWidgetState(context, ""); 
        } 
        @Override 
        public void onReceive(Context paramContext, Intent paramIntent) 
         { 
         String str = paramIntent.getAction(); 
         if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_RECEIVER)) { 
          updateWidgetState(paramContext, str); 
         } 
         else 
         { 
           if ("android.appwidget.action.APPWIDGET_DELETED".equals(str)) 
            { 
            int i = paramIntent.getExtras().getInt("appWidgetId", 0); 
            if (i == 0) 
            { 

            } 
            else 
            { 
             int[] arrayOfInt = new int[1]; 
             arrayOfInt[0] = i; 
             onDeleted(paramContext, arrayOfInt); 
            } 
            } 
          super.onReceive(paramContext, paramIntent); 
         } 
         } 
        static void updateWidgetState(Context paramContext, String paramString) 
         { 
         RemoteViews localRemoteViews = buildUpdate(paramContext, paramString); 
         ComponentName localComponentName = new ComponentName(paramContext, MyWidget.class); 
         AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews); 
         } 
        private static RemoteViews buildUpdate(Context paramContext, String paramString) 
         { 
         // Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show(); 
         rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout); 
         Intent active = new Intent(paramContext, MyWidget.class); 
         active.setAction(ACTION_WIDGET_RECEIVER); 

         PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0); 

     // upadte this R.id.buttonus1 with your layout or image id on which click you want to start Activity 

    Intent configIntent = new Intent(paramContext, Caller2.class); 
    configIntent.setAction((ACTION_WIDGET_CLICK_RECEIVER); 
    PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, configIntent, 0); 
rview.setOnClickPendingIntent(R.id.Phonestatexx, configPendingIntent); 
         if(parmString.equals(ACTION_WIDGET_CLICK_RECEIVER)) 
         { 

          //open Activity here.. 
         //your code for update and what you want on button click 
      // 

         } 
         return rview; 
         } 
        @Override 
        public void onEnabled(Context context){ 
         super.onEnabled(context); 
         // Toast.makeText(context, "onEnabled() ", Toast.LENGTH_SHORT).show(); 
        } 
        // Called each time an instance of the App Widget is removed from the host 
        @Override 
        public void onDeleted(Context context, int [] appWidgetId){ 
         super.onDeleted(context, appWidgetId); 
         // Toast.makeText(context, "onDeleted() ", Toast.LENGTH_SHORT).show(); 
        } 
        // Called when last instance of App Widget is deleted from the App Widget host. 
        @Override 
        public void onDisabled(Context context) { 
         super.onDisabled(context); 
         // Toast.makeText(context, "onDisabled() ", Toast.LENGTH_SHORT).show(); 
        } 

       } 
+0

喜denza看到我的解決辦法。這是在我身邊的工作示例,如果您有任何問題,然後說我在運行此代碼 – 2012-03-12 17:48:04

+1

Toast.makeText(paramContext,「nesto tost」,Toast.LENGTH_SHORT).show(); 我試圖把敬酒下,如果(parmString.equals(ACTION_WIDGET_CLICK_RECEIVER)) ,但它並沒有顯示,我不能創建一個新的意圖意圖(這一點,Caller2.class)就說明這個 – denza 2012-03-13 09:12:22

+0

錯誤,您可以更新您的發佈最新代碼 – 2012-03-13 09:31:19

相關問題