2012-03-30 12 views
3

私はボタンで構成されるトグルウィジェットに取り組んでいます。押されたら、何かを開かずに活動を実行したい(ちょうどいつものようにデスクトップで言う)。デスクトップウィジェットのボタンを押してアクティビティを直接実行する方法はありますか?ありがとう! 更新:新しいアクティビティを実行せずに、コード内からサイレントモードを切り替えるようにしようとしています。Androidのウィジェットのボタンからアクティビティを実行するにはどうすればよいですか?

方法1: はここに私の現在のコード(ボタンは、私はいくつかの理由のためにそれらをクリックして何もしない)ホーム画面のウィジェットをクリックしたときに、

package toggleHD.widget; 

import android.app.Activity; 
import android.app.IntentService; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.media.AudioManager; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.RemoteViews; 
import android.widget.Toast; 


    public class ButtonWidget 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, ButtonWidget.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.main); 
      Intent active = new Intent(paramContext, ButtonWidget.class); 
      active.setAction(ACTION_WIDGET_CLICK_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, TestReceiver.class); 
configIntent.setAction(ACTION_WIDGET_CLICK_RECEIVER); 
PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, configIntent, 0); 
      if(paramString.equals(ACTION_WIDGET_CLICK_RECEIVER)) 
      { 

       //open Activity here.. 
      //your code for update and what you want on button click 
// 
     rview.setOnClickPendingIntent(R.id.button_one, configPendingIntent); 
      } 
      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

ホーム画面ウィジェットであなたはアクティビティを開始しますか? –

+0

はい。しかし、私はそれがフルスクリーンのページまたは何か、ただの活動を開くことを望んでいません。 – MattDementous

+0

アクティビティは通常、全画面表示です。ダイアログテーマなどにテーマを指定しない限り、 – Martyn

答えて

7

開始アクティビティである上でonReceive

Intent intentClick = the new Intent to (context, update, class); 
PendingIntent pendingIntent = PendingIntent.getActivity (context, 0, 
intentClick, 0); 
rv.setOnClickPendingIntent (R.id.layout, pendingIntent); 

方法2:onReceive

Intent intn = new Intent (context, update. Class); 
intn.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity (intn); 

あなたはウィジェットのために放送を登録する必要がありますクリック

+0

そして私のウィジェットにボタンがあればどうしますか?私はどのようにこれらを実装しますか? – MattDementous

+0

私の答えを見てください:[自分のウィジェットがクリックされたときにアクティビティを開始しますか?](http://stackoverflow.com/questions/9671596/how-do-i-start-an-activity-when-my-widget-is-clicked/9681914#9681914) –

+0

そして、具体的にどのアクティビティを実行するかをどのように伝えますか? (リンクしたあなたのポストを使用) – MattDementous

1

あなたがしたい場合は、バックグラウンドでは、アクティビティの代わりにサービスを使用することができます。 http://developer.android.com/reference/android/app/Service.html

1.起動するか、ウィジェットが表示されたらすぐにstarServiceできます。バインドさhttp://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent

widgeが http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent、android.content.ServiceConnection、INTをクリックするbindServiceに2.try)

3.after、あなたはserviceConnectionを使用して、バインダーからサービスインスタンスを取得することができます。次に、サービスで定義されているコードを実行することができます。 ここでは例を参照します。http://developer.android.com/reference/android/app/Service.html

またはあなたがする唯一のサービスインスタンス上で(サービスを開始するだけで

  1. 試すことができます

  2. サービスにsthを依頼するサービスをブロードキャストしてください。あなたのために。

アクティビティはsthをユーザーに表示するように設計されており、サービスはバックグラウンドで実行するように設計されているためです。

関連する問題