android 中的后台服务生成数据库更改通知

Background service in android that generates notifications on database change

我正在尝试编写一个在后台运行并重复调用用 PHP 编写的 Web 服务的服务。这个网络服务 returns 我 JSON。我收到 JSON 但无法发出通知。

我的来源:

public class RepetedHttpCallService extends Service {

    private static String TAG = RepetedHttpCallService.class.getSimpleName();
    private MyThread mythread;
    public boolean isRunning = false;
    NotifyServiceReceiver notifyServiceReceiver;
    Notification noti;

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

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");   

        mythread  = new MyThread();
    }
    @Override
    public synchronized void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        if(!isRunning){
            mythread.interrupt();
            mythread.stop();
        }       
    }
    @Override
    public synchronized void onStart(Intent intent, int startId) {
        super.onStart(intent, startId); 
        Log.d(TAG, "onStart");
        if(!isRunning){
            mythread.start();
            isRunning = true;
        }
    }

    class MyThread extends Thread{
        static final long DELAY = 5000;
        @Override
        public void run(){          
            while(isRunning){
                Log.d(TAG,"Running");
                try {                   
                    readWebPage();
                    Thread.sleep(DELAY);
                } catch (InterruptedException e) {
                    isRunning = false;
                    e.printStackTrace();
                }
            }
        }

    }

    public void readWebPage(){

        try {
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://10.0.2.2:91/xxxxxxx/webServices/getValue.php");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost,responseHandler);
        if(!response.equals(""))
        {
            createNotification();
        }
        Log.e("log_tag","POST URL response "+response.toString());

        } catch (Exception e) {
             e.printStackTrace();
        }
    }

    @SuppressLint("NewApi")
    public void createNotification() {
         int counter=0;
         NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


            System.out.println("in notification");
           Intent intent = new Intent(this, MainActivity.class);
          // Intent intent1=new Intent(this,SendNotification.class);
            PendingIntent pIntent = PendingIntent.getActivity(getBaseContext(), counter, intent,  0);//FLAG_CANCEL_CURRENT,
                                                                                    //FLAG_ONE_SHOT,
                                                                                    //FLAG_UPDATE_CURRENT
            //PendingIntent pIntent1 = PendingIntent.getActivity(this, counter, intent1, 0);
            counter++;

            String c=String.valueOf(counter);
            noti = new Notification.Builder(getApplicationContext())
                .setContentTitle("New mail from " + "test@gmail.com")
                .setContentText("Sub "+counter)
                .setContentIntent(pIntent)
                .addAction(R.drawable.ic_launcher, "make", pIntent)
                .setTicker("got notification "+counter)
                .build();

            noti.flags = Notification.FLAG_AUTO_CANCEL;


            notificationManager.notify(counter, noti);

    }
}

服务正在启动,每 5 秒向我显示 JSON。当我在一个单独的线程中调用它时,我知道我在这里出错了,因为我需要传递上下文。但我不确定该怎么做。 还有其他方法可以实现这一目标吗?

您需要为通知设置一个图标。 您可以使用

进行设置

.setSmallIcon() 更新后的代码段变为

noti = new Notification.Builder(getApplicationContext())
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Sub "+counter)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "make", pIntent)
.setTicker("got notification "+counter)
.setSmallIcon(R.drawable.ic_launcher)
.build();
 noti.flags = Notification.FLAG_AUTO_CANCEL;`