当应用未运行时,未收到短信已收到的broadcastReceiver

时间:2016-09-08 09:12:04

标签: android android-broadcastreceiver smsmanager

我正在使用broadcastReceiver来收听即使应用未运行时收到的短信。它在 Kitkat 上工作正常,甚至应用程序已从最近的应用程序中移除,但不在 Lollipop 及更高版本的设备上。

In Manifest它定义如下。

 <receiver
        android:name=".receivers.MxSMSReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter android:priority="5822">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.provider.Telephony.SMS_SENT" />
        </intent-filter>
    </receiver>

收件人代码:

public class MxSMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {

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

   }
  }
 }

我不知道是什么问题。任何帮助表示赞赏

2 个答案:

答案 0 :(得分:1)

完整的工作演示运行时权限

更改您希望在运行时获取的权限名称。

private String permission = Manifest.permission.RECEIVE_SMS;

  

您希望在运行时获得的那些权限也应在声明中声明   也表现出来。

public class MainActivity extends AppCompatActivity {

    private Context context;
    private Button button;
    private static final int REQUEST_RUNTIME_PERMISSION = 123;
    private String permission = Manifest.permission.RECEIVE_SMS;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                if (CheckPermission(MainActivity.this, permission)) {
                    // you have permission go ahead
                    YouCanReadContactNow();
                } else {
                    // you do not have permission go request runtime permissions
                    RequestPermission(MainActivity.this, permission, REQUEST_RUNTIME_PERMISSION);
                }
            }
        });
    }

    private void YouCanReadContactNow() {
    }


    @Override
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

        switch (permsRequestCode) {
            case REQUEST_RUNTIME_PERMISSION: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // you have permission go ahead
                    YouCanReadContactNow();
                } else {
                    // you do not have permission show toast.
                }
                return;
            }
        }
    }

    public void RequestPermission(Activity thisActivity, String Permission, int Code) {
        if (ContextCompat.checkSelfPermission(thisActivity,
                Permission)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                    Permission)) {
            } else {
                ActivityCompat.requestPermissions(thisActivity,
                        new String[]{Permission},
                        Code);
            }
        }
    }

    public boolean CheckPermission(Context context, String Permission) {
        if (ContextCompat.checkSelfPermission(context,
                Permission) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }
}

<强>布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="horizontal">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:text="Request contact permissions"
        android:textSize="20dp" />

</RelativeLayout>

答案 1 :(得分:0)

Make sure from the following :

 android:minSdkVersion="your min"
 android:targetSdkVersion="your max" />

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