2014-07-12 65 views
0
public class SimpleService extends Service { 
private NotificationManager mNM; 
private int NOTIFICATION = 0; 

public class LocalBinder extends Binder { 
     SimpleService getService() { 
      return SimpleService.this; 
     } 
    } 
@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();   
} 

@Override 
public void onDestroy() { 
    // Cancel the persistent notification. 
    mNM.cancel(NOTIFICATION); 
    // Tell the user we stopped. 
    Toast.makeText(this,"Service is destroy", Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    super.onCreate(); 
    Toast.makeText(this,"Service started", Toast.LENGTH_LONG).show(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Toast.makeText(this,"task perform in service",300).show(); 
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    // Display a notification about us starting. We put an icon in the status bar. 
    showNotification(); 
    return START_STICKY; 
} 

    // This is the object that receives interactions from clients. See 
    // RemoteService for a more complete example. 
    private final IBinder mBinder = new LocalBinder(); 

    /** 
    * Show a notification while this service is running. 
    */ 
    private void showNotification() { 
     // In this sample, we'll use the same text for the ticker and the expanded notification 
     CharSequence text = getText(R.string.local_service_started); 

     // Set the icon, scrolling text and timestamp 
     Notification notification = new Notification(R.drawable.ic_launcher, text, 
       System.currentTimeMillis()); 
     Intent intent = new Intent(this,NotificationRecieverActivity.class); 
     // The PendingIntent to launch our activity if the user selects this notification 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0); 

     // Set the info for the views that show in the notification panel. 
     notification.setLatestEventInfo(this, "latest information", 
         text, contentIntent); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     // Send the notification. 
     mNM.notify(NOTIFICATION, notification); 
    } 

}如果點擊通知,如何打開anoter活動?

如果我點擊通知,然後該通知是明確的,但我不會顯示此通知消息到另一個活動即NotificationRecieverActivity.class但這不顯示任何內容。請幫幫我。

+0

您應該閱讀本手冊,它並不總是像提供的簡單答案一樣線性(例如,您可能想要提供返回導航以維護應用程序體驗) http://developer.android.com /guide/topics/ui/notifiers/notifications.html – Philio

回答

0

嘗試的意圖類似下面

Intent a=new Intent(this,NewActivity.class); 
a.putExtra("var", "your message"); //value passing 
startActivity(a); 

並獲得NewActivity類的消息,如

try { 
    Intent i = getIntent();    
    String message = i.getStringExtra("var");   
}catch (Exception ex) { 
    Log.e("Error", "Loading exception"); 
} 
+0

我正在編寫此代碼,但發生異常。 07-12 16:41:33.460:E/AndroidRuntime(1753):java.lang.RuntimeException: 無法使用Intent {cmp = com.bogotobogo.serviceprovider/.SimpleService}啓動服務[email protected] : android.util.AndroidRuntimeException: 從Activity上下文外部調用startActivity()需要FLAG_ACTIVITY_NEW_TASK標誌。這真的是你想要的嗎? –

+0

是否改變了你的Activity類名? – MH2K9

+0

通過創建對象在活動類和SimpleService類中使用意圖! – MH2K9

3

執行以下操作

Intent intent = new Intent(this,NotificationRecieverActivity.class); 
intent.putExtra("YOURTAG", "DATA"); 
    // The PendingIntent to launch our activity if the user selects this notification 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0); 

或使用捆綁

Intent intent = new Intent(this,NotificationRecieverActivity.class); 
Bundle bundle = new Bundle(); 
bundle.putString("YOURTAG", "DATA"); 
intent.putExtra("BUNDLETAG", bundle); 
// The PendingIntent to launch our activity if the user selects this notification 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0); 

全類

public class GcmIntentService extends IntentService { 
private NotificationManager mNotificationManager; 

public GcmIntentService() { 
    super("GcmIntentService"); 
} 
public static final String TAG = "Mobien Reception Service"; 

@Override 
protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    // The getMessageType() intent parameter must be the intent you received 
    // in your BroadcastReceiver. 
    String messageType = gcm.getMessageType(intent); 

    if (!extras.isEmpty()) { // has effect of unparcelling Bundle 
     /* 
     * Filter messages based on message type. Since it is likely that GCM will be 
     * extended in the future with new message types, just ignore any message types you're 
     * not interested in, or that you don't recognize. 
     */ 
     if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
      makeMessage("Send error: " + extras.toString()); 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { 
      makeMessage("Deleted messages on server: " + extras.toString()); 
     // If it's a regular GCM message, do some work. 
     } else { 
      Log.d(TAG, "Received Message :"+extras.getString("message")); 
      makeMessage(extras.getString("message")); 
     } 
    } 
    // Release the wake lock provided by the WakefulBroadcastReceiver. 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 

private void makeMessage(String msg) { 

    if(!msg.equals("") || msg.contains("#")) { 
     String temp [] = StringUtility.split(msg, '#'); 
     String header = temp[0]; 
     Log.d(TAG, "Header Message :"+header); 
     if(header.trim().contains("DLV")) { 
      sendNotification("Del. No. "+temp[1], "Against SAP SO.No. "+temp[2], 123); 
     } else if(header.trim().contains("PGI")) { 
      sendNotification("PGI No. "+temp[1], "Against Del. No."+temp[2], 99); 
     } else if(header.trim().contains("INV")) { 
      sendNotification("Inv. No. "+temp[1], "Against Del. No."+temp[2], 157); 
     } 
    } 
} 

// Put the message into a notification and post it. 
// This is just one simple example of what you might choose to do with 
// a GCM message. 
private void sendNotification(String contentTitle, String contentText) { 

    final Random r = new Random(); 
    final int notificationId = r.nextInt(); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, LoginActivity.class), 0); 

    Bitmap largeIcon= BitmapFactory.decodeResource(getApplicationContext().getResources(), 
      R.drawable.ic_launcher); 

    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setLargeIcon(largeIcon) 
    .setContentTitle(contentTitle) 
    .setContentText(contentText) 
    .setAutoCancel(true) 
    .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)); 


    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(notificationId, mBuilder.build()); 
} 

它的東西我已經爲我的演示製作。我沒有時間根據您的需要進行修改。但它應該幫助你解決你在做的事情。創作的PendingIntent後

notification.setContentIntent(contentIntent); 

-

+0

我正在編寫此代碼,但發生異常 - 07-12 16:56:34.450:E/AndroidRuntime(1932):java.lang.RuntimeException: 無法啓動服務[email protected] with Intent { cmp = com.bogotobogo.serviceprovider/.SimpleService} java.lang.IllegalArgumentException: contentView required:pkg = com.bogotobogo.serviceprovider id = 0 notification = Notification(pri = 0 contentView = null vibrate = null sound = null defaults = 0x0 flags = 0x0 kind = [null]) –

+0

根據你在這裏做什麼,你真的不需要擴展服務的類。改用IntentService。 使用創建具有意圖調用的通知的服務更新答案。 – iZBasit

+0

我不會顯示通知,如果應用程序不是在設備上運行的狀態。如果任何更新應用程序中顯示更新通知 –

0

只需添加您創建掛起的意圖,以通知的。

+0

Setcontentintent();此方法不可用。但我設置--- notification.setLatestEventInfo(這個,「最新信息」, \t text,contentIntent); 方法。 –

+0

'Notification notification = new Notification(R.drawable.ic_launcher,text, System.currentTimeMillis());'由於此方法已被棄用,最好使用NotificationBuilder來創建通知。 –

+0

這裏是使用NotificationBuilder創建通知的一個例子'NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context); notificationBuilder.setTicker(「Ticker Text」); \t notificationBuilder.setAutoCancel(true); \t notificationBuilder.setContentIntent(pendingIntent); \t notificationBuilder.setContentTitle(「content title」).setContentText(「content text」)。setSmallIcon(R.drawable.ic_launcher); \t mNm.notify(NOTIFICATION_ID,notificationBuilder.build());' –

相關問題