我正在尝试使用AlarmManager和Service
实现警报应用程序在我的活动中,我已声明以下内容以启动警报服务
Intent intent = new Intent(getApplicationContext(), OnAlarmManager.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
Toast.makeText(getApplicationContext(), "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();
我的OnAlarmManager代码如下所示
public class OnAlarmManager extends Service{
private static final String TAG = "OnAlarmReceiver";
private NotificationManager nm;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent mainActivityIntent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, 0);
//setting up notification
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("New Notification")
.setContentText("This is a successfull app on service")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
nm.notify(0, notification);
Toast.makeText(getApplicationContext(), "Alarm has begun", Toast.LENGTH_LONG).show();
Log.i(TAG, "Alarm has begun");
return START_STICKY;
}
}
现在,当我尝试运行此操作时,即使我在设置警报后关闭应用程序,也会在JellyBean上运行的设备中的给定时间触发通知。 但是在Marshmallow上运行的设备中,应用程序关闭时永远不会触发服务类
我在这里缺少什么?
答案 0 :(得分:1)
你能试试前台服务吗? http://www.vogella.com/tutorials/AndroidServices/article.html
根据你的说法,服务似乎在主线程上运行,它会与应用程序一起被杀死。
答案 1 :(得分:0)
为此,请尝试将AlarmManager.setExact
与IntentService
一起使用。您的代码将如下所示:
Intent intent = new Intent(OnAlarmManager.ACTION_TRIGGER_ALARM);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Toast.makeText(getApplicationContext(), "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
}
您的服务将类似于:
public class OnAlarmManager extends IntentService {
public static final String ACTION_TRIGGER_ALARM = "com.example.myapplication.ACTION_TRIGGER_ALARM";
private static final String TAG = "OnAlarmReceiver";
private NotificationManager nm;
public OnAlarmManager() {
super("OnAlarmManager");
}
@Override
protected void onHandleIntent(Intent intent) {
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent mainActivityIntent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, 0);
//setting up notification
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("New Notification")
.setContentText("This is a successfull app on service")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
nm.notify(0, notification);
Log.i(TAG, "Alarm has begun");
}
}
并在AndroidManifest.xml中添加操作信息:
<service
android:name=".OnAlarmManager"
android:exported="true">
<intent-filter>
<action android:name="com.example.myapplication.ACTION_TRIGGER_ALARM" />
</intent-filter>
</service>
答案 2 :(得分:-1)
只需在Manifest.xml文件中注册服务
即可<service android:name=".YourServiceClassName" />