2014-07-03 229 views
1

我需要通知服务调用功能可按,我尝试:Android通知行动服务

意向cancelIntent =新意图(这一点,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 ----”); }

我该如何做到这一点?

我在相同的服务中创建通知!!!!

谢谢!

回答

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 登录.d(“AlarmReceiver”,“从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是将被打开一次点击notofication任何活动

谢谢