如何将活动带到前台? BroadcastReceiver内部没有任何工作

时间:2012-02-13 01:30:15

标签: android foreground

我有一个接收短信的活动,我希望在发生这种情况时将其带到前台。如果它是同一个实例或新实例并不重要。我已经尝试过在这个网站上找到的所有东西,甚至像FLAG_ACTIVITY_NEW_TASK这样的其他标志也能正常工作,但是[到目前为止] NOTHING将活动带到了前台。

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);
    }
}

2 个答案:

答案 0 :(得分:0)

我相信你要找的是这个。创建您的接收器作为单独的文件(MyBroadcastReceiver.java)并让它启动活动或弹出通知或其他。在你的AndroidManifest中试试这个:

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

另外,请确保在AndroidManifest中包含短信权限:

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

答案 1 :(得分: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);

它对我有用..!

当我们点击通知时,活动就会被带到前面。