2012-06-30 5 views
19

私は2つのボタンを持つアプリケーションを持っています。アプリケーションを「閉じる」ボタンと、アルゴリズムを開始するボタン。 「開始」をクリックすると、アプリケーションが非表示になり、通知バーに通知が表示されます。私は、通知がクリック/押されたときにメソッドを実行/呼び出すことができる必要があります。この種の質問にはいくつか答えがありますが、それは信じられないほど曖昧であり、BroadcastReceiverの文書へのリンクを指しているだけです。通知をクリックしてメソッドを実行する方法

BroadcastReceiverのドキュメントにURLを残して「このページを読む」と言う場合は、この質問に返信しないでください。 BroadcastReceiverを使用して(通知を表示したクラスと同じクラス内から)メソッドを実行する方法を説明する場合は、これを実行する方法のコードを表示してください。

アルゴリズム:ボタンを押し、通知を表示し、通知をクリックし、メソッドを呼び出します(アクティビティを表示しません)。それでおしまい。

それができない場合は、私に知らせてください。そうであれば、それを可能にするためにあなたがすることを私に見せてください。この単純なことは、アンドロイドSDKの開発者が見落としてはいけないはずです。

+0

私は、通知バーに通知を通知して表示する通知マネージャーしか手に入れませんでした。通知をクリックすると、アクティビティが開きます。この動作を変更するには、アクティビティを開く代わりにメソッドを呼び出したり実行したりしていました。私はArfinのソリューションで作業しようとしています。私はこれをどうやって起こさせるのか分かりません。私は "DummyActivity"を作成しました。彼のソリューションの2番目の部分が "DummyActivity"になっているかどうかはわかりません。私はちょうど混乱しています。私は物事がスムーズに合うときが好きです。 – AnDev

+0

yea AnDevあなたのダミーアクティビティが開始されたら、それらからブロードキャストを送信して終了します。親クラスでブロードキャストメッセージを受け取ります。すでに説明したように、そこから任意のメソッドを呼び出すことができます。放送を送信して終了することを除いて、ダミーアクティビティの中でさらに何もする必要はありません。 –

答えて

8

火災イベントまたは任意のクリックリスナーを取得することはできません。通知バーでアプリケーションを追加する際に、通常はアクティビティを開始する保留中のインテントを通知するために使用します。しかし、私は本当にあなたの活動を表示したくない場合は保留中の意図から始めようとしている活動をあなたの親活動に広範なキャストを送信し、親アクティビティで、受信者の中で必要なメソッドを呼び出します。あなたの参考のために、試行錯誤の数回の反復の後に..

// This is what you are going to set a pending intent which will start once 
// notification is pressed. Hopes you know how to add notification bar. 
Intent notificationIntent = new Intent(this, dummy_activity.class); 
notificationIntent.setAction("android.intent.action.MAIN"); 
notificationIntent.addCategory("android.intent.category.LAUNCHER"); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
           notificationIntent, 
           PendingIntent.FLAG_UPDATE_CURRENT | 
           Notification.FLAG_AUTO_CANCEL); 

// Now, once this dummy activity starts send a broad cast to your parent activity and finish the pending activity 
//(remember you need to register your broadcast action here to receive). 
    BroadcastReceiver call_method = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       String action_name = intent.getAction(); 
       if (action_name.equals("call_method")) { 
        // call your method here and do what ever you want. 
       } 
      }; 
     }; 
     registerReceiver(call_method, new IntentFilter("call_method")); 
    } 
} 
+0

お返事ありがとうございます。それは非常に徹底して有用です。 – AnDev

+0

アプリケーションがフォアグラウンドであるか、通知クリックイベントでないかをチェックしたい場合は、コード@Daud Arfinに従って、アクティビティを起動すると常にアプリケーションがフォアグラウンドに戻ります。アプリケーションがフォアグラウンドにあるかどうかを確認する別の方法はありますか? –

+0

@ Kim Kimi Dhingra - あなたはアンドロイドライフサイクルに依存することができます。onPause/onStopとonResume/onStartの中にフラグを維持します。 –

-3
protected void displayNotification() { 
    Log.i("Start", "notification"); 

    /* Invoking the default notification service */ 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this); 
    mBuilder.setAutoCancel(true); 

    mBuilder.setContentTitle("New Message"); 
    mBuilder.setContentText("You've received UnRead message."); 
    mBuilder.setTicker("New Message Alert!"); 
    mBuilder.setSmallIcon(R.drawable.icon2); 

    /* Increase notification number every time a new notification arrives */ 
    mBuilder.setNumber(++numMessages); 

    /* Creates an explicit intent for an Activity in your app */ 

    Intent resultIntent = new Intent(this, FreesmsLog.class); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    stackBuilder.addParentStack(FreesmsLog.class); 

    /* Adds the Intent that starts the Activity to the top of the stack */ 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
        0, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    mBuilder.setContentIntent(resultPendingIntent); 
    mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    /* notificationID allows you to update the notification later on. */ 
    mNotificationManager.notify(notificationID, mBuilder.build()); 

} 
+9

Googleのサンプルコードのコピー貼り付けのようです。 OPの質問には答えません。 – cdkn7

27

、私は最終的に、通知のアクションがクリックされたときに、任意の方法を実行するためにかなり簡単でクリーンな方法を見つけました。私のソリューションでは、通知を作成する1つのクラス(NotificationUtilsと呼ぶ)があり、通知のアクションがクリックされたときに実行されるIntentService静的内部クラスも含まれています。

public class NotificationUtils { 
    public static final int NOTIFICATION_ID = 1; 

    public static final String ACTION_1 = "action_1"; 

    public static void displayNotification(Context context) { 

     Intent action1Intent = new Intent(context, NotificationActionService.class) 
      .setAction(ACTION_1); 

     PendingIntent action1PendingIntent = PendingIntent.getService(context, 0, 
       action1Intent, PendingIntent.FLAG_ONE_SHOT); 

     NotificationCompat.Builder notificationBuilder = 
       new NotificationCompat.Builder(context) 
         .setSmallIcon(R.drawable.ic_launcher) 
         .setContentTitle("Sample Notification") 
         .setContentText("Notification text goes here") 
         .addAction(new NotificationCompat.Action(R.drawable.ic_launcher, 
           "Action 1", action1PendingIntent)); 

     NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); 
     notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); 
    } 

    public static class NotificationActionService extends IntentService { 
     public NotificationActionService() { 
      super(NotificationActionService.class.getSimpleName()); 
     } 

     @Override 
     protected void onHandleIntent(Intent intent) { 
      String action = intent.getAction(); 
      DebugUtils.log("Received notification action: " + action); 
      if (ACTION_1.equals(action)) { 
       // TODO: handle action 1. 
       // If you want to cancel the notification: NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID); 
      } 
     } 
} 

今だけonHandleIntentであなたの行動を実装し、<application>タグ内にマニフェストにNotificationActionServiceを追加します:

<service android:name=".NotificationUtils$NotificationActionService" /> 

ここでのAndroidManifest.xmlに必要な変更に続いて、私のNotificationUtilsクラスは、あります要約:

  • 通知を作成するクラスを作成します。
  • このクラスの中で、IntentServiceの内部クラス(が静的​​であるか、または暗黙のエラー!)を追加して、クリックされたアクションに基づいてメソッドを実行できます。
  • マニフェストでIntentServiceクラスを宣言します。
+0

アクションフィールドが既に使用されている場合はどうなりますか? 「ACTION_VIEW」のように –

+0

インテントに余分なものを追加することができます。 onHandleIntentでは、次のように使用します。intent.getIntExtra( "action1extra"、-1)== 1){...} –

+0

これまでのところ、これを行う方法。私はあなたの答えを見つけてうれしいです、魅力のように動作します。 – kalenpw

関連する問題