微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

杀死应用程序后,Alarmmanager 可以在物理设备上运行,但不能在模拟器上运行

如何解决杀死应用程序后,Alarmmanager 可以在物理设备上运行,但不能在模拟器上运行

我正在尝试制作闹钟。我的 MainActivity 中的 AlarmManager 应该设置一个警报,该警报将在指定时间后发送给接收器,接收器将触发服务。问题是,当我从最近删除我的应用程序时,它在模拟器上不起作用,但在我的手机上一切正常。 (这也有些奇怪,因为该应用程序在我的手机上被杀死后仍可运行,但前提是我的手机未通过 android studio 连接到我的笔记本电脑)。我的模拟手机是带有 API 30 的 Pixel 2,而我的真实手机是带有 API 29 的三星 S9。

这是带有 AlarmManager 的 MainActivity 部分:

AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(getApplicationContext(),broadcastReciver.class);
        PendingIntent pendingIntent = PendingIntent.getbroadcast(getApplicationContext(),intent,0);
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 5000,pendingIntent);

接收方:

public class broadcastReciver extends broadcastReceiver {

    @Override
    public void onReceive(Context context,Intent intent) {
        Log.i("AAAAA","broadcast");
        Toast.makeText(context,"broadcast",Toast.LENGTH_SHORT).show();
        intent = new Intent(context,AlarmService.class);
        context.startService(intent);
    }


}

服务:

public class AlarmService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //Toast.makeText(this,"Service",Toast.LENGTH_SHORT).show();
        return null;
    }

    @Override
    public int onStartCommand(Intent intent,int flags,int startId) {
        Log.i("AAAAAAA","Service");
        Toast.makeText(this,Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent,flags,startId);
    }
}

清单:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alarmclock">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".broadcastReciver"/>
        <service android:name=".AlarmService"
            android:exported="true"
            android:enabled="true"/>
    </application>

</manifest>

编辑:

我尝试在没有应用程序的情况下启动模拟器,但我得到了这些日志:

2020-12-27 23:44:45.237 526-553/? W/broadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.DROPBox_ENTRY_ADDED flg=0x10 (has extras) } to com.google.android.gms/.stats.service.DropBoxEntryAddedReceiver
2020-12-27 23:44:45.238 526-553/? W/broadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.DROPBox_ENTRY_ADDED flg=0x10 (has extras) } to com.google.android.gms/.chimera.GmsIntentOperationService$PersistentTrustedReceiver
2020-12-27 23:44:48.623 191-195/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-12-27 23:44:48.627 191-195/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-12-27 23:44:48.619 191-191/? W/Binder:191_2: type=1400 audit(0.0:288): avc: denied { read } for name="wakeup34" dev="sysfs" ino=18285 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
2020-12-27 23:44:48.623 191-191/? W/Binder:191_2: type=1400 audit(0.0:289): avc: denied { read } for name="wakeup35" dev="sysfs" ino=18345 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
2020-12-27 23:44:53.117 526-856/? D/WifiNl80211Manager: Scan result ready event
2020-12-27 23:44:53.117 526-856/? D/WifiNative: Scan result ready event
2020-12-27 23:44:56.060 526-541/? E/JobScheduler.Background: App com.google.android.gms became active but still in NEVER bucket

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。