当app不在后台时,ACTION_USER_PRESENT无效

时间:2016-08-12 07:29:11

标签: android broadcastreceiver android-6.0-marshmallow

我在我的应用程序中使用ACTION_USER_PRESENT广播接收器, 我的问题是:我的应用程序处于暂停状态时才会收到BroadCastReceiver。

这是我的清单:

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


<receiver
            android:name=".utils.receivers.ReminderBroadcastReceiver"
            android:enabled="true"
            android:exported="true" />
        <receiver android:name=".utils.receivers.UserPresentBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

我的接收者:

public class UserPresentBroadcastReceiver extends BroadcastReceiver {


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

        /*Sent when the user is present after
         * device wakes up (e.g when the keyguard is gone)
         * */

       // MY STUFF - which works when my app is on paused state, but not when it is closed

    }

}

我在Marshmallo 6.0上运行

任何帮助?

1 个答案:

答案 0 :(得分:0)

获取Marshmallo 6.0或更好的运行时权限。

public class RuntimePermission extends AppCompatActivity {

        private static final int REQUEST_RUNTIME_PERMISSION = 123;

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

            if (CheckPermission(RuntimePermission.this, Manifest.permission.READ_PHONE_STATE)) {
                // you have permission go ahead

            } else {
                // you do not have permission go request runtime permissions
                RequestPermission(RuntimePermission.this, Manifest.permission.READ_PHONE_STATE, REQUEST_RUNTIME_PERMISSION);
            }
        }

        @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

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