刷通知导致服务停止工作android

时间:2017-08-08 11:16:08

标签: android broadcastreceiver alarmmanager localnotification notificationmanager

目前,通知会在设定的时间以一天的间隔出现。但是,当我向右或向左滑动通知或从通知栏中删除通知时,警报不会在第二天重复,并且通知会不时地弹出,如果我继续刷它。

  

主要活动中指定的setNotification功能

public void setNotification(int hour, int minutes, int ampm){

    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minutes);
    calendar.set(Calendar.SECOND, 0);
    if(ampm == 1) {
        calendar.set(Calendar.AM_PM, Calendar.PM);
    }else{
        calendar.set(Calendar.AM_PM, Calendar.AM);
    }

    Intent myIntent = new Intent(MainActivity.this, notificationReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 60000 ,pendingIntent);

    SharedPreferences.Editor mEditor = mPrefs.edit();
    alarm_set_flag = mEditor.putBoolean(tag, true).commit();

}
  

alarmService.JAVA

package com.silveroakhealth.tranquil;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;

/**
 * Created by Varun K Nair on 7/7/2017.
 */

public class alarmService extends Service {
    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }
    @Override
    public void onCreate()
    {
        Uri sound = Uri.parse("android.resource://"+this.getPackageName()+"/"+R.raw.bell);
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Intent intent = new Intent(this.getApplicationContext(),MainActivity.class);
        Intent deleteIntent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
        PendingIntent clearIntent = PendingIntent.getActivity(this,0,deleteIntent,0);

        Notification notification = new Notification.Builder(this)
                .setContentTitle("Tranquil")
                .setContentText("Pause. It’s time to be mindful!")
                .setSmallIcon(getNotificationIcon())
                .setContentIntent(pendingIntent)
                .setDeleteIntent(clearIntent)
                .setAutoCancel(true)
                .setColor(getResources().getColor(R.color.notification))
                .setSound(sound)
                .build();
        notificationManager.notify(0,notification);
    }

    private int getNotificationIcon() {
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.notification_sil : R.drawable.notification_icon;
    }

}
  

notificatioReciever.JAVA

package com.silveroakhealth.tranquil;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Created by Varun K Nair on 7/7/2017.
 */

public class notificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, alarmService.class);
        context.startService(service1);

    }
}
  

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.silveroakhealth.tranquil">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:screenOrientation="portrait">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".alarmService"
            android:enabled="true" />

        <receiver android:name=".notificationReceiver"
            android:enabled="true">
        </receiver>
    </application>

</manifest>

有人可以告诉我出了什么问题以及如何在不取消警报的情况下每天重复警报。

0 个答案:

没有答案