Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从服务发送通知_Java_Android_Android Studio - Fatal编程技术网

Java 从服务发送通知

Java 从服务发送通知,java,android,android-studio,Java,Android,Android Studio,如果我从活动发送通知,则通知发送的实现实际上是有效的,但如果我从服务发送通知,则不会发生任何事情。有人能提出可能的解决办法吗?谢谢 这是我的服务实现 public class MyService extends Service { private NotificationManager notificationManager; private DBHelper dbHelper; private SQLiteDatabase db; private Context mContext = thi

如果我从
活动
发送通知,则通知发送的实现实际上是有效的,但如果我从服务发送通知,则不会发生任何事情。有人能提出可能的解决办法吗?谢谢

这是我的
服务
实现

public class MyService extends Service {

private NotificationManager notificationManager;
private DBHelper dbHelper;
private SQLiteDatabase db;
private Context mContext = this;

private int notificationID = 100;

@Override
public void onCreate() {
    Log.i("myLOgs", "Service: onCreate()");
    super.onCreate();
    dbHelper = new DBHelper(this);
    db = dbHelper.getWritableDatabase();
    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));
        boolean dateCompare = currentDateString.equals(dateString);
        boolean timeCompare = currentTimeString.equals(timeString);
        if((dateCompare) && (timeCompare)) {
            Notify(eventString, "message");
        }
        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();
}



private void Notify(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, MainActivity.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);
}
}

您还没有告诉我们当您尝试从
服务发送时会发生什么情况。。。请详细说明您的问题,如果您遇到异常,可能会使用stacktrace。这是我的日志,说明的不多,所以我假设您没有遇到异常。你确定调用了
onStartCommand
,如果是,你确定调用了
Notify
方法吗?@Darwind“Notify”方法是在我删除行(if(dateCompare)&&(timeCompare))时调用的,它会返回数据库表中我拥有的每个条目,这意味着,你的if语句没有按应有的方式运行。。。