Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

Android Broadcast SMS

I want open App when receive SMS. I try to handle this problem using Manifest-declared receivers.

Here is my code

<AndroidManifest.xml>
    
    <receiver
        android:name=".service.SMSReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
  @Override
public void onReceive(Context context, Intent intent) {

    packageManager = context.getPackageManager();

    if("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())) {

        Bundle bundle = intent.getExtras();
        Object[] messages = (Object[]) bundle.get("pdus");
        for (Object pdu : messages)
        {
            SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
            if(message == null)
            {
                Log.e(TAG,"message is null");
                break;
            }
            smsSender = message.getDisplayOriginatingAddress();
            if(smsSender.compareTo(number)==0)
            {
                receivedData = new Date(message.getTimestampMillis());

                smsBody = message.getDisplayMessageBody();
                Log.i(TAG, "onReceive: "+smsBody);
                SendToActivity(context,smsSender,smsBody,receivedData);
            }

        }
    }
}
 private void SendToActivity(Context context, String sender, String contents, Date receivedDate) {
    Log.i(TAG, "SendToActivity: TEST ");

    Intent intent = new Intent(context, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|
            Intent.FLAG_ACTIVITY_SINGLE_TOP|
            Intent.FLAG_ACTIVITY_CLEAR_TOP);

    intent.putExtra("contents",contents);
    context.startActivity(intent);
    Log.i(TAG, "SendToActivity: RUN >>  ");
    
}

this code works only when app is onPause(). I want to work even app is terminated.

Is possible that terminated app open automatically when SMS received at Android 9?

question from:https://stackoverflow.com/questions/65838089/android-broadcast-sms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, you can't do this. There are certain limitations that Android had put on the Broadcast Receivers.

There are certain broadcasts that Apps can't listen to, and it includes the SMS broadcast too.

You can find more about here

There are some broadcasts which your app can listen to when terminated, you can find the list here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...