如何获取通知甚至应用程序在Android中关闭

时间:2016-03-12 04:21:11

标签: android service notifications background-process

我正在开发一个每五秒钟触发一次通知的应用程序,但我需要这样做,即使应用程序被关闭或被杀,就像什么应用程序和gmail通知一样...要运行通知我正在使用该服务但是当我正在关闭或杀死应用程序通知未来可以任何人告诉我如何做到这一点 这就是我所做的 这是我的活动:

    package com.example.servicesandroid;


    import java.util.Timer;
    import java.util.TimerTask;

    import android.app.Activity;
    import android.app.ActivityManager;
    import android.app.ActivityManager.RunningServiceInfo;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.Button;


    public class MainActivity extends Activity {

            Timer timer;
            Handler handler;
            TimerTask timer_task;

         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             handler = new Handler();
             setContentView(R.layout.activity_main);
             Button buttonStartService = (Button)findViewById(R.id.startservice);
             Button buttonStopService = (Button)findViewById(R.id.stopservice);
             timer = new Timer();
             timer_task = new TimerTask() {

                @Override
                public void run() {

                    Intent intent = new Intent(MainActivity.this, Androidservice.class);
                      MainActivity.this.startService(intent);
                }
            };
timer.schedule(timer_task, 5,5000);

这是我的服务:

package com.example.servicesandroid;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;

public class Androidservice extends Service {

    final static String ACTION = "NotifyServiceAction";
    final static String STOP_SERVICE = "";
    final static int RQS_STOP_SERVICE = 1;

    NotifyServiceReceiver notifyServiceReceiver;

    private static final int MY_NOTIFICATION_ID = 1;
    private NotificationManager notificationManager;
    private Notification myNotification;
    private final String myBlog = "http://sample.com/";

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        notifyServiceReceiver = new NotifyServiceReceiver();
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION);
        registerReceiver(notifyServiceReceiver, intentFilter);

        // Send Notification
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        myNotification = new Notification(R.drawable.ic_launcher,
                "Notification!", System.currentTimeMillis());
        Context context = getApplicationContext();
        String notificationTitle = "Notification!";
        String notificationText = "http://sample.com/";
        Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
        PendingIntent pendingIntent = PendingIntent.getActivity(
                getBaseContext(), 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
        myNotification.defaults |= Notification.DEFAULT_SOUND;
        myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
        myNotification.setLatestEventInfo(context, notificationTitle,
                notificationText, pendingIntent);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

        return  Service.START_STICKY;
    }



    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        this.unregisterReceiver(notifyServiceReceiver);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public class NotifyServiceReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            int rqs = arg1.getIntExtra("RQS", 0);
            if (rqs == RQS_STOP_SERVICE) {
                stopSelf();


            }
        }
    }

}

3 个答案:

答案 0 :(得分:0)

您正在使用主活动中的计时器来启动通知。当您关闭应用程序时,计时器会停止,通知也会停止。将定时器的工作转移到服务本身内部。这将有助于您启动通知,而不依赖于活动。

答案 1 :(得分:0)

您正在从活动权限内触发定时器任务。当app关闭时,活动会被破坏,你的任务也会被破坏。尝试更改服务中的触发机制。

答案 2 :(得分:0)

每次启动计时器时,您似乎都会启动该服务。相反,将计时器逻辑转移到服务并仅启动服务一次。如果您需要更多说明,请询问。