Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.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
Android通知操作到服务_Android_Cordova - Fatal编程技术网

Android通知操作到服务

Android通知操作到服务,android,cordova,Android,Cordova,我需要从通知到服务调用一个函数。以下是我尝试过的: Intent cancelIntent = new Intent(this, MyService.class); cancelIntent.putExtra("close", "true"); cancelIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent piCancel = PendingIntent

我需要从通知到服务调用一个函数。以下是我尝试过的:

Intent cancelIntent = new Intent(this, MyService.class);
cancelIntent.putExtra("close", "true");
cancelIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent piCancel = PendingIntent.getActivity(getApplicationContext(), 0, cancelIntent, Intent.FLAG_ACTIVITY_CLEAR_TASK);
mBuilder.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.close), piCancel);
但公共服务商没有接到电话

public IBinder onBind(Intent intent) {
    System.out.println("MYSERVICE onBind ----");
}
我正在尝试在同一服务中创建通知。 我怎样才能让它工作


谢谢

您可以按以下方式使用接收器和服务:

1创建重复频率:

    private void setRecurringAlarm(Context context) {
    Calendar updateTime = Calendar.getInstance();
    updateTime.setTimeZone(TimeZone.getDefault());
    updateTime.set(Calendar.HOUR_OF_DAY, 12);
    updateTime.set(Calendar.MINUTE, 30);
    Intent downloader = new Intent(context, BroadcastReceiverService.class);
    downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(),  30 * 1000/*AlarmManager.INTERVAL_FIFTEEN_MINUTES*/, pendingIntent);

    Log.d("MyActivity", "Set alarmManager.setRepeating to: " + updateTime.getTime().toLocaleString());
}
2创建ReceiverService类

public class BroadcastReceiverService  extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent dailyUpdater = new Intent(context, MyService.class);
    context.startService(dailyUpdater);
//context.stopS Log.dAlarmReceiver,从AlarmReceiver.onReceive调用context.startService; } }

3单击通知时创建通知和相关操作:

public class MyService extends IntentService {
public MyService() {
    super("MyServiceName");
}
@Override
protected void onHandleIntent(Intent intent) {
    Log.d("MyService", "About to execute MyTask");
    new MyTask().execute();
    this.sendNotification(this);
}
private class MyTask extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... strings) {
        Log.d("MyService - MyTask", "Calling doInBackground within MyTask");
        return false;
    }
}

private void sendNotification(Context context) {
    Intent notificationIntent = new Intent(context, SupportActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification =  new Notification(android.R.drawable.star_on, "New Message...", System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(context, "Message Support Title","Content Message text", contentIntent);
    notificationMgr.notify(0, notification);
}
}

4 SupportActivity是指一旦单击标记就会打开的任何活动

谢谢