gpt4 book ai didi

java - 如何通过服务发送通知

转载 作者:行者123 更新时间:2023-11-30 02:40:59 24 4
gpt4 key购买 nike

我正在尝试通过服务发送通知。通知时间是从我有日期、时间等列的数据库表中设置的。因此,当 System.currentTimeMillis 等于来自数据库列的时间时,我试图将通知发送给用户。但是我的实现不起作用,有人可以帮助我吗?提前致谢

这是我的服务文件

public class MyService extends Service {

private NotificationManager notificationManager;
private DBHelper dbHelper;
private SQLiteDatabase db;

@Override
public void onCreate() {
Log.i("myLOgs", "Service: onCreate()");
super.onCreate();
dbHelper = new DBHelper(this);
db = dbHelper.getReadableDatabase();
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}

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

Log.i("myLOgs", "Service: onStartCommand()");

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String currentDateString = dateFormat.format(date);

SimpleDateFormat timeFormat = new SimpleDateFormat("HH-mm");
Date time = new Date();
String currentTimeString = timeFormat.format(time);

String[] columns = {DBHelper.DATE, DBHelper.TIME, DBHelper.EVENT};
Cursor cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
cursor.moveToFirst();
do {
String dateString = cursor.getString(cursor.getColumnIndex(DBHelper.DATE));
String timeString = cursor.getString(cursor.getColumnIndex(DBHelper.TIME));
String eventString = cursor.getString(cursor.getColumnIndex(DBHelper.EVENT));
if((currentDateString.equals(dateString)) && (currentTimeString.equals(timeString))) {
Notification notification = new Notification(R.drawable.ic_launcher, eventString, System.currentTimeMillis());
}
if(cursor.moveToLast()) {
cursor.moveToFirst();
}
}while(true);

//return super.onStartCommand(intent, flags, startId);
}

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

@Override
public void onDestroy() {
super.onDestroy();
}
}

最佳答案

您可以简单地使用此方法从服务发出通知。

只需将此方法作为您的服务类的成员,然后您就可以从服务类 onStartCommand 或任何您需要的地方调用此方法。

我在我的项目中使用的完全正常工作......

  private int notificationID = 100;

private void giveNotification(String notificationTitle, String bodytext)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.ic_launcher, "New Message", System.currentTimeMillis());

Intent notificationIntent = new Intent(this, DeliveryReport.class);

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationID, notificationIntent, 0);

notification.setLatestEventInfo(getApplicationContext(), notificationTitle, bodytext, pendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationID++, notification);
}

关于java - 如何通过服务发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25761433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com