Android 如何将活动带到前台?广播接收机内部没有工作

Android 如何将活动带到前台?广播接收机内部没有工作,android,foreground,Android,Foreground,我有一个活动,接收短信,我想它被带到前台时,发生了。不管是同一个实例还是新实例。我已经尝试了我在这个网站上找到的所有东西,甚至像FLAG_ACTIVITY_NEW_TASK这样的其他标志也可以很好地工作,但是[到目前为止]没有任何东西能将活动带到前台 public class SMSMessenger extends Activity { private static final String ACTION = "android.provider.Telephony.SMS_RECEIV

我有一个活动,接收短信,我想它被带到前台时,发生了。不管是同一个实例还是新实例。我已经尝试了我在这个网站上找到的所有东西,甚至像FLAG_ACTIVITY_NEW_TASK这样的其他标志也可以很好地工作,但是[到目前为止]没有任何东西能将活动带到前台

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

我相信你要找的就是这个。将你的接收者创建为一个单独的文件(MyBroadcastReceiver.java),让它启动活动或弹出一个通知或任何东西。在您的AndroidManifest中,尝试放置以下内容:

<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" />

试试这个

    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);
这对我很有用


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

我试过了,如果弹出窗口指的是敬酒,那么效果很好。但我想要的是我的活动打开/恢复。我实际上是指通知。请参阅:我已经有一段时间没有使用广播接收器了,我记得现在您不能使用它们绑定到服务/启动活动。创建具有要启动的活动的挂起意图的通知。