"所有应用关闭后的Android通知"按钮

时间:2017-03-24 20:45:51

标签: android notifications alarmmanager

我在我的应用程序中使用通知并且一切正常 - 即使在设备重启后,通知也会在所需的时间显示给我。

但是,当我点击特殊按钮关闭所有应用时,它会关闭我的应用,通知根本不会显示。

"关闭所有应用的特殊按钮" - 我的意思是你可以列出所有后台应用程序,然后只需点击一下就可以杀死它们。

我在OnePlus 3T上测试所有内容。

之后是否可以再次运行该服务"杀死所有应用程序"点击?

清单:

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

    ...

        <receiver
            android:name="com.saga.thewitcherquotes.QuoteOfTheDayReceiver"
            android:enabled="true"
            android:exported="false" >
        </receiver>

        <receiver android:name="com.saga.thewitcherquotes.DeviceBootReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.saga.thewitcherquotes.QuoteOfTheDayIntentService"
            android:exported="false"></service>

BootReceiver:

public class DeviceBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            // on device boot compelete, reset the alarm
            Intent alarmIntent = new Intent(context, QuoteOfTheDayReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

            Calendar current = Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 21);
            calendar.set(Calendar.MINUTE, 30);
            if(calendar.before(current))
            {
                calendar.add(Calendar.DATE, 1);
            }

            manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY, pendingIntent);
        }
    }
}

带有通知构建器的服务:

public class QuoteOfTheDayIntentService extends IntentService {
    private static final int NOTIFICATION_ID = 3;


    public QuoteOfTheDayIntentService() {
        super("MyNewIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle(getString(R.string.quote_of_the_day));
        DatabaseHandler db = DatabaseHandler.getInstance(this);
        Random generator = new Random();
        int id = generator.nextInt(db.getQuotesSize());
        Quote quote = db.getQuote(id, null);
        builder.setContentText(quote.getQuote());
        builder.setSmallIcon(R.drawable.swords);
        builder.setAutoCancel(true);
        Intent notifyIntent = new Intent(this, MainActivity.class);
        notifyIntent.putExtra("id",id-1);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        Notification notificationCompat = builder.build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(NOTIFICATION_ID, notificationCompat);
    }
}

0 个答案:

没有答案