2012-02-13 6 views
-1

私はSMSを受け取るアクティビティを持っています。それが起こると、フォアグラウンドに持ち込みたいと思います。同じインスタンスか新しいインスタンスかは関係ありません。私はFLAG_ACTIVITY_NEW_TASKのような他のフラグでもうまく動作しますが、このサイトで見つけたものすべてを試しましたが、[今のところ] NOTHINGはアクティビティをフォアグラウンドに持ち込みます。アクティビティをフォアグラウンドに持ち込む方法は? BroadcastReceiverの内部から何もできません

public class SMSMessenger extends Activity { 
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; 
    private BroadcastReceiver yourReceiver; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     final IntentFilter theFilter = new IntentFilter(); 
     theFilter.addAction(ACTION); 

     this.yourReceiver = new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context context, Intent intent) { 

       // Bring activity to the foreground - doesn't work 
       Intent I = new Intent(context, SMSMessenger.class); 
       I.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       context.startActivity(I); 
       //------------------------------------------------ 
      } 
     }; 
     this.registerReceiver(this.yourReceiver, theFilter); 
    } 
} 

答えて

0

私はあなたが探しているものはこれだと信じています。レシーバーを別々のファイル(MyBroadcastReceiver.java)として作成し、アクティビティーを起動するか、通知などをポップアップします。

<receiver android:name=".MyBroadcastReceiver"> 
    <intent-filter> 
     <action android:name= 
      "android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver> 

また、あなたがあなたのAndroidManifestにおけるSMSの権限が含まれていることを確認します:

<uses-permission android:name="android.permission.RECEIVE_SMS" /> 
+0

私はそれを試しました。ポップアップによってあなたがトーストを意味するならば、それはうまくいきます。しかし、私が望むのは、自分の活動が開かれ/再開することです。 – user1204946

+0

私は実際にNotificationsを参照していました。 http://developer.android.com/guide/topics/ui/notifiers/notifications.html – Bill

+0

私は放送受信機を扱っているので、しばらくして、サービスにバインドしたり、活動を開始することはできません。 起動したいアクティビティの保留中の意図で通知を作成します。 – Bill

0

は、それは私のために働い

myManager  = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    myNotification = new Notification(R.drawable.alert1, "ALERT...!!", System.currentTimeMillis()); 
    myIntent  = new Intent(context, SmsMessanger.class); 
    myPendingIntent = PendingIntent.getActivity(context, 0, myIntent, 0); 
    myNotification.setLatestEventInfo(context, "NEW SMS ALERT...!!", "You have a new SMS alert..!! Click here to stop the alert tone...!!", myPendingIntent); 
    myManager.notify(0, myNotification); 

...これを試してみてください。あなたのAndroidManifestでこれを入れてみてください!

通知をクリックすると、アクティビティが前面に表示されます。

関連する問題