2014-07-03 122 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任何活動

謝謝