应用程序关闭时,活动中的预期服务已停止

时间:2015-03-10 19:20:44

标签: java android android-intent android-activity service

我有一个服务chatService,它会在MainActivity启动时启动。即应用程序运行时。

即使应用关闭,我也希望始终运行此服务。但不幸的是,当我关闭我的应用程序时,服务停止。我想在android启动或我的应用程序启动时始终在后台运行newMsg()。但是当应用程序关闭时它会关​​闭。

有人能指出我正确的方向吗?

MainActivity.java

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intService =  new Intent(MainActivity.this, ChatHeadService.class);
       startService(intService);
    }
}

chatService.java

public class chatService extends Service {      
        public void onCreate() {
            super.onCreate();
            new newMsg(this,null,null,null).execute(); 
       }
     ...
}

newMsg.java

protected void onPreExecute() {
     super.onPreExecute();  
}   

protected JSONArray doInBackground(Object... v) {
...
}
protected void onPostExecute(JSONArray json) {
    ...
    new Handler().postDelayed(new Runnable() {
        public void run() {
            new newMsg(main,...,...,...).execute();
        }
    }, 10000);      
}

更新1

将我的new newMsg(this,10,null,null,null).execute();转移到onStartCommand()并添加了return START_STICKY

猜猜是什么?

它没有任何区别!

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    new newMsg(this,10,null,null,null).execute();
    return START_STICKY;
}

2 个答案:

答案 0 :(得分:1)

您可以使用RETURN START STICKY 我使用的是智能手机,因此无法发布代码。

搜索RETURN START STICKY

public class Serv extends Service {

    String t,msg1;
    int id;

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

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

        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
//ConnectionDetector  is custom class for detecting internet connection
//isCD() returns true if internet connection available


                if(cd.isCD())    
                   getData();

            }
        }, 0,360000);


        return START_STICKY;

    }

答案 1 :(得分:0)

通过在chatService.java文件上替换它,我能够做我想要的事情。但它始终将通知保持在最顶层。任何能告诉我如何在没有通知的情况下实现这一点的人都会有所帮助。

<强> chatService.java

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

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

    //This constructor is deprecated. Use Notification.Builder instead
    Notification notice = new Notification(R.drawable.ic_launcher, "Ticker text", System.currentTimeMillis());

    //This method is deprecated. Use Notification.Builder instead.
    notice.setLatestEventInfo(this, "Title text", "Content text", pendIntent);

    notice.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(myID, notice);

    return super.onStartCommand(intent, flags, startId);
    //return Service.START_STICKY;
}