0

I'm trying to make a notification from a service. the code goes as follows

UpdateService.java

package com.androidhive.jsonparsing;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class UpdateService extends Service {

    private Updater updater;
    private static final String TAG = "Background";
    private boolean isRunning = false;
    public static int id = 1;
    NotificationManager NM;
    JSONArray updates = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public boolean isRunning() {
        return this.isRunning;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this.getApplicationContext());
        Editor prefEditor = prefs.edit();
        prefEditor = prefs.edit();
        prefEditor.putString("old", "0");
        prefEditor.putString("new", "");
        prefEditor.commit();
        updater = new Updater();
    }

    @Override
    public synchronized void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        if (!this.isRunning) {
            updater.start();
            this.isRunning = true;
        }
        Log.d(TAG, "ON START");
    }

    @Override
    public synchronized void onDestroy() {
        super.onDestroy();
        if (this.isRunning) {
            updater.interrupt();
        }
        updater = null;
        Log.d(TAG, "ON DESTOY");
    }

    class Updater extends Thread {

        static final long DELAY = 30000;

        @Override
        public void run() {
            isRunning = true;
            while (isRunning) {
                try {
                    Log.d(TAG, "update is running");
                    JSONParser jparser = new JSONParser();
                    JSONObject json1 = jparser
                            .getJSONFromUrl("http://ensignweb.com/sandbox"
                                + "/app/comment11.php");
                    updates = json1.getJSONArray("products");
                    JSONObject update = updates
                            .getJSONObject(updates.length() - 1);
                    String cid = update.getString("cid");
                    Log.d("update", "new update is available");
                    SharedPreferences prefs1 = PreferenceManager
                            .getDefaultSharedPreferences(UpdateService.this
                                    .getApplicationContext());
                    String old1 = prefs1.getString("old", "");
                    if (!(old1.equals(cid))) {
                        old1 = cid.toString();
                        Intent intent = new Intent(UpdateService.this,
                                AndroidJSONParsingActivity.class);
                        PendingIntent pIntent = PendingIntent.getActivity(
                            UpdateService.this, 0, intent, 0);
                        Notification n = new Notification(
                                R.drawable.ic_launcher, "new update arrived",
                                System.currentTimeMillis());
                        n.setLatestEventInfo(getApplicationContext(), "Update",
                            "new update arrived", pIntent);
                        NM = (NotificationManager) getSystemService
                                (Context.NOTIFICATION_SERVICE);
                        NM.notify(id, n);
                    }
                    Log.d("Update", "new value is");
                    // }
                    Thread.sleep(DELAY);
                } catch (InterruptedException e) {
                    isRunning = false;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

But I'm confused where to place the notication.close() function so that once the user clicks the notification, the required activity must start and the notification must be removed. I tried all kinds of thigs. But none worked. Please suggest.

4

0 回答 0