Этим утром я нашел некрасивое решение своей проблемы с помощью Alarmmanager, поэтому даже если приложение не запущено, пользователь все равно будет уведомлен. Вот мой код:
package com.android.my.hotnews;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.Cursor;
public class CommentNotificationService extends Service{
private Notification notifyMe;
private NotificationManager notifyManager;
private PendingIntent pIntent;
private Intent intent;
private static String DBPATH="data/data/com.android.my.hotnews/databases/";
private static String DBNAME="mydb.db";
private String path = DBPATH+DBNAME;
private Timer timer = new Timer();
private static final long UPDATE_INTERVAL = 5000;
String title = null;
String content = null;
String date = null;
Calendar cal = null;
public void onCreate(){
super.onCreate();
notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifyMe = new Notification(R.drawable.comment_icon, "New Comment", System.currentTimeMillis());
intent = new Intent(android.content.Intent.ACTION_VIEW, null, getApplicationContext(), CommentViewer.class);
pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
inComingComment();
}
public void inComingComment() throws SQLiteException{
SQLiteDatabase db;
db=SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
Cursor rs = db.rawQuery("SELECT * FROM Comment_Log ORDER BY Comment_Date_Time ASC", null);
if(rs.getCount()>0){
rs.moveToFirst();
try {
String[] dmy = rs.getString(1).split("/");
String[] hm = rs.getString(2).split(":");
cal = Calendar.getInstance();
cal.set(Calendar.DATE, Integer.parseInt(dmy[0]));
cal.set(Calendar.MONTH, Integer.parseInt(dmy[1])-1);
cal.set(Calendar.YEAR, Integer.parseInt(dmy[2]));
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hm[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(hm[1]));
title="new comment";
content=rs.getString(3);
db.close();
} catch (Exception e) {
log.d("error ocurred",e.getClass().getName());
db.close();
}
}
timer.schedule(new TimerTask(){
@Override
public void run() {
//here i set time for AlarmManager
Intent intent = new android.content.Intent(getApplicationContext(), CommentReceiver.class);
intent.putExtra("Title", title);
intent.putExtra("Content", content);
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent);
}
}, 0, UPDATE_INTERVAL);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
ниже мой класс CommentReceiver
package com.android.my.hotnews;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.PendingIntent;
import android.app.Notification;
import android.app.NotificationManager;
public class CommentReceiver extends BroadcastReceiver{
private NotificationManager nm;
private Notification notify;
@Override
public void onReceive(Context context, Intent intent) {
String title = intent.getExtras().get("Title").toString();
String content = intent.getExtras().get("Content").toString();
Intent notifyIntent = new Intent(context, CommentViewer.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, notifyIntent, 0);
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notify = new Notification(R.drawable.app_icon, "Incoming comment", System.currentTimeMillis());
notify.setLatestEventInfo(context, title, content, pending);
nm.notify(1, notify);
}
}
Кстати, я новичок в разработке android, поэтому извините, если мой вопрос неясен. Я не думаю, что это лучший способ, так как сервис может зависать при нехватке памяти. Поэтому я должен напрямую использовать AlarmManager вместо вызова AlarmManager в сервисе, если нет взаимодействия между пользователем и приложениями.