2015-06-08 21 views
-3

我有一个任务,在该任务中,我必须使用BroadcastReceiver以及SMS上的地址和内容来显示Notification中的传入消息。现在我必须点击才能在数据库中插入sms值。当任务完成后,我希望它从活动中清除。是否有可能在活动内部为传入的短信创建通知?如何在android中的活动内部创建通知?

+0

是的,这是可能的。现在的问题是什么? –

+0

必须在活动内部显示传入消息的通知。 –

+0

您正在询问密码。关于如何生成通知有一千个答案,您只需要了解方式。 Btw答案是由某人发布。 –

回答

0

您可以试试这不会生成您自己的通知。

@SuppressWarnings("deprecation") 
private static void generateNotification(Context context) { 

    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, "New Contact Added", 
      when); 

    String title = context.getString(R.string.app_name); 

    Intent notificationIntent = new Intent(context, MainActivity.class); 

    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff); 

    PendingIntent intent = PendingIntent.getActivity(context, iUniqueId, 
      notificationIntent, 0); 
    notification.setLatestEventInfo(context, title, "New", intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Play default notification sound 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(2, notification); 

} 
+0

这是'BroadcastReceiver'里面的工作吗? –

+0

是的当你将消息接收器消息,里面,它会工作 –