AndroidStudio定时器AlarmManager在真机测试失效?求助!求解!

在虚拟机上测试很OK,能够在设置的延时时间产生效果,但是搬到真机就收不到效果,没有反应!贴代码!大家帮忙看看~~
AndroidManifest.xml的权限啊,接收器啊都有

 <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.BIND_DEVICE_ADMIN"
        tools:ignore="ProtectedPermissions" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".activity.ScheduleActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>
        <activity android:name=".activity.ScheduleAddActivity">activity>
        <receiver android:name=".activity.ScheduleAddActivity$AlarmReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <intent-filter>
                <action android:name="com.example.schedule.activity.ScheduleAddActivity.AlarmReceiver"/>
            intent-filter>
        receiver>

activity.java的部分关键代码如下

					Intent intent=new Intent(ALARM_EVENT);//闹钟服务意图
	                //用于广播的延迟意图,广播闹钟服务
	                PendingIntent pendIntent=PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
	                AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);//从系统服务中获取闹钟服务
	                Calendar calendar=Calendar.getInstance();
	                if(alarm_type==6){
     //延时提醒,当前时间之后10秒提醒
	                    calendar.setTimeInMillis(System.currentTimeMillis());
	                    calendar.add(Calendar.SECOND,3);//delay_time[alarm_type]
	                }else {
     //提前提醒
	                    calendar.set(2021,month,day,hour,minute,0);
	                    calendar.add(Calendar.SECOND,-delay_time[alarm_type]*60);
	                }
	                //设置闹钟,1.模式是睡眠状态下也叫醒,2.延迟时间、3.携带的广播意图
	                alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendIntent);
	                Log.d("当前时间秒",calendar.getTimeInMillis()+"");

这里是接收处理代码

    private String ALARM_EVENT="com.example.schedule.activity.ScheduleAddActivity.AlarmReceiver";
    public static   class AlarmReceiver extends BroadcastReceiver{
     
        @Override
        public void onReceive(Context context, Intent intent) {
     
            mvibrator.vibrate(3000);
            schedule_date.setText("收到收到");
            Vibrator vibrator= (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                vibrator.vibrate(3000);//震动3秒
            if(intent!=null){
     
                Toast.makeText(context,"alarmreceive!",Toast.LENGTH_LONG).show();
            }
        }
    }

然而最后也没有收到。。。

你可能感兴趣的:(android,app,定时任务)