Android Notification对服务的操作

时间:2014-07-03 10:12:39

标签: 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.getActivity(getApplicationContext(), 0, cancelIntent, Intent.FLAG_ACTIVITY_CLEAR_TASK);
mBuilder.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.close), piCancel);

但是服务的公共IBinder没有接到电话。

public IBinder onBind(Intent intent) {
    System.out.println("MYSERVICE onBind ----");
}

我正在尝试在同一服务中创建通知。 我怎样才能使这个工作?

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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)创建BroadcastReceiverService类

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.d(" AlarmReceiver","从AlarmReceiver.onReceive&#34中调用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是点击notofication后将打开的任何活动

由于