繁体   English   中英

使用 android 10 中的警报管理器杀死应用程序后的本地通知

[英]Local Notification after App killed using Alarm manager in android 10

我想在特定时间显示本地通知。 这样我就可以使用警报管理器来设置特定时间的待处理意图。 但在我的情况下,如果应用程序被用户杀死,则不会调用广播/服务。

检查以下代码并帮助我了解为什么在应用程序终止后没有收到通知。

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        

        Intent notifyIntent = new Intent(this,MyReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast
                (MainActivity.this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,  System.currentTimeMillis()+30000, pendingIntent);
    }
}
public class MyReceiver extends BroadcastReceiver {

    public MyReceiver() {
        
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        
  
        Intent intent1 = new Intent(context, MyNewIntentService.class);
        context.startService(intent1);
    }
}
public class MyNewIntentService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        CommonUtil.showNotification(getApplicationContext());
        return START_STICKY;
    }
}

AndroidManifest.xml


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

  <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="false"
            />

        <service
            android:name=".MyNewIntentService"
            android:exported="false"
            />

您可以在此处查看带有广播接收器的警报的工作示例。

如何在 Kotlin 的 Fragment 中使用 Android AlarmManager?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM