2016-02-28 92 views
0

我有关于创建方法的通知中的代码,但我想要在应用程序关闭时显示通知。比如像Messenger一样,当你连接到wifi甚至没有打开Messenger通知即将到来。Android:当应用程序关闭时显示通知

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_take_photo_or_upload); 

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Image"); 
    query.orderByDescending("createdAt"); 
    query.whereEqualTo("recipientUsername", ParseUser.getCurrentUser().getUsername()); 

    query.findInBackground(new FindCallback<ParseObject>() { 
     public void done(List<ParseObject> images, ParseException e) { 

      if (e == null) { 

       if (images.size() > 0) { 

        for (final ParseObject image : images) { 

         ParseFile file = image.getParseFile("image"); 
         byte[] byteArray = new byte[0]; 
         try { 
          byteArray = file.getData(); 
          bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
         } catch (ParseException e1) { 
          e1.printStackTrace(); 
         } 

         PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1, new Intent(getApplicationContext(), ViewPhotos.class), 0); 
         Resources r = getResources(); 
         Notification notification = new NotificationCompat.Builder(getApplicationContext()) 
           .setLargeIcon(bitmap) 
           .setTicker("You have new photo incoming!") 
           //.setSmallIcon(R.drawable.ic_stat) 
           .setSmallIcon(R.drawable.logopt) 
           .setContentTitle("PhotoTSFun") 
           .setContentText(image.getString("sendUsername") + " has sent you an image") 
           .setContentIntent(pi) 
           .setAutoCancel(true) 
           .build(); 

         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
         notificationManager.notify(0, notification); 

        } 

       } 
      } 
     } 
    }); 

}

感谢。

+0

在服务中实现代码并使服务在后台连续运行。 –

回答

0

1)您必须检测当你的应用程序中的活动应用程序类关闭 一)对破坏或方法的onStop B)onterminate。

2)从那里意图与出版通知在onstartcommand/onhandleintent 放代码发送到服务

3)在服务。

相关问题