简体   繁体   中英

How to create running application in Background android?

I create simple notification in Android, this notification always check data from server when there new input data will display notification in device, I've used intentservice and setting androidManifest but still not display notification when there new data input from server, this is my code :

Class Notification // to get notification in background

public class Notification extends IntentService {
    private UrlHelper Url = new UrlHelper();
    private JSONParser jsonParser = new JSONParser();
    public Notification() {
        super(Notification.class.getName());
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        new Notif().start();}
    private class Notif extends Thread{
        @Override
        public void run() {
            try {
                while (true){
                    Thread.sleep(3000);
                    String jsonStr = new String();
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    if(UserData.update_id==null){UserData.update_id= 0;}
                    jsonStr = jsonParser.makeHttpRequest(Url.updatesUrl(UserData.update_id), "GET", params);
                    try {
                        JSONObject json = new JSONObject(jsonStr);
                        JSONArray data = json.getJSONArray("otg");
                        Log.d("cek", UserData.cek + "");
                        for(int i=0;i<data.length();i++) {
                            JSONObject otg = data.getJSONObject(i);
                            String ID   = otg.getString("id");
                            String desc = otg.getString("Desc");
                            String type = otg.getString("Category");
                            UserData.update_id = Integer.parseInt(ID);
                            if (UserData.cek!=null){
                                fireNotif(type+"-"+desc);

                            }else{}}
                        UserData.cek = UserData.update_id;

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

AndroidManifest

Ok you just declared your service in AndroidManifest.xml and this is ok, but you need start this service too.

In Activity in onCreate() method you could start this service:

startService(new Intent(this, Notif.class));

2'nd problem is - that IntentService live is short - ie IntentService work until handleIntent() method is finish. In your case your service works only few ms, because you create new Thread and handleMethod is finished.

My proposition is: Don't create new Thread. IntentService make new thread for you and you don't need create new Thread. Put all your logic in handleIntent() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM