Activity 的预期服务在应用关闭时停止

Intended Service from Activity stopped when app is closed

我有一项服务 chatService,它会在 MainActivity 启动时启动。即当应用 运行s.

我想 运行 即使在应用程序关闭时也始终使用此服务。但不幸的是,当我关闭我的应用程序时,服务停止了。当 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;
}

您可以使用 RETURN START STICKY 我正在使用智能手机,所以无法 post 代码。

搜索RETURN开始置顶

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;

    }

通过在 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;
}