2014-07-12 67 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());' –

相关问题