Android:关闭应用时显示通知

时间:2016-02-28 14:15:34

标签: java android

我在Create方法上有关于通知的代码,但我想在应用关闭时显示通知。例如,像Messenger一样,当你连接到wifi甚至甚至不打开Messenger通知即将到来。

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);

                    }

                }
            }
        }
    });

}

感谢。

1 个答案:

答案 0 :(得分:0)

1)当您的应用关闭时,您必须检测   a)关于破坏或停止方法的活动   b)在申请类别中终止。

2)从那里发送意图服务

3)在onstartcommand / onhandleintent服务 将代码放入发布通知。