简体   繁体   English

如何在后台android中创建正在运行的应用程序?

[英]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 : 我在Android中创建了一个简单的通知,当有新的输入数据将在设备中显示通知时,此通知将始终从服务器检查数据,我使用了intentservice并设置了androidManifest,但从服务器有新的数据输入时仍未显示通知,这是我的代码:

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 AndroidManifest

Ok you just declared your service in AndroidManifest.xml and this is ok, but you need start this service too. 好的,您只是在AndroidManifest.xml中声明了您的服务,这没关系,但是您也需要启动此服务。

In Activity in onCreate() method you could start this service: 在Activity中的onCreate()方法中,您可以启动此服务:

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

2'nd problem is - that IntentService live is short - ie IntentService work until handleIntent() method is finish. 第二个问题是-IntentService有效期很短-即IntentService工作到handleIntent()方法完成为止。 In your case your service works only few ms, because you create new Thread and handleMethod is finished. 在您的情况下,您的服务仅工作数毫秒,因为您创建了新的Thread并且handleMethod已完成。

My proposition is: Don't create new Thread. 我的主张是:不要创建新的线程。 IntentService make new thread for you and you don't need create new Thread. IntentService为您创建新线程,而您无需创建新线程。 Put all your logic in handleIntent() method 将所有逻辑放入handleIntent()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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