stopService()方法不停止服务

时间:2015-02-02 10:25:27

标签: android service ondestroy

当调用活动退出时,我从调用类调用了stopService()方法,并且服务没有停止。它继续在后台运行,因此可以成为最终用户的一个支柱。任何帮助将不胜感激..

代码:

public class MainActivity2 extends ActionBarActivity {
ListView listView;
static int cnt1=0;
static int bob=1;
RelativeLayout background2;


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main_activity2);



background2 = (RelativeLayout) findViewById(R.id.background);
int num = 0;
final ListView theListView = (ListView) findViewById(R.id.listView);
Intent calledActivity = getIntent();
final List pe = calledActivity.getExtras().getStringArrayList("Caller1");

String[] s = new String[pe.size()];
for (int i = 0; i < pe.size(); i++) {
    s[i] = (String) pe.get(i);
}

final ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pe);
theListView.setAdapter(theAdapter);
final int cnt = 1;
Intent mIntent = new Intent(this, MyService.class);
mIntent.putStringArrayListExtra("Caller2", (ArrayList<String>) pe);
mIntent.putExtra("intCall",bob);
startService( mIntent);



theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String s1 = String.valueOf(parent.getItemAtPosition(position));
        pe.remove(s1);
        if (pe.size() == 0) {
            stopService(new Intent(MainActivity2.this, MyService.class));

            Intent goback;
            goback = new Intent(MainActivity2.this, MainActivity.class);
            startActivity(goback);
        }
        ((BaseAdapter) theAdapter).notifyDataSetChanged();


    }
});
}

我的服务类:

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent arg0) {

return null;}

@Override
public void onCreate() {

    Toast.makeText(this, "Congrats! Blocker Created", Toast.LENGTH_LONG).show();

}

@Override
public void onStart(final Intent intent, int startId) {
int i=0;

    Timer t = new Timer();

    t.scheduleAtFixedRate(new TimerTask() {

                              @Override
                              public void run() {
                                  ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                                  List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);


                                  ActivityManager.RunningTaskInfo ar = tasks.get(0);
                                  String activityOnTop=ar.topActivity.getClassName();
                                  Intent lockIntent = new Intent(MyService.this, LockScreen.class);
                                  lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                  ComponentName componentInfo = tasks.get(0).topActivity;
                                  if(!componentInfo.getPackageName().equals("com.example.epiclapser.noprocrastinate"))
                                  {

                                        startActivity(lockIntent);
                                      Log.v("iwashere", "-- ACTIVITY --");
                                  }
                              }


                          },
            0,

            1000);

    Toast.makeText(this, "My Blocker Started", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
    Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
    super.onDestroy();
}


}

3 个答案:

答案 0 :(得分:2)

服务中的onStart()是一种描述方法,您应该使用onStartCommand。请参阅此链接here。它还声明onStart默认设置&#34; START_STICKY&#34;标志,导致服务在被杀后自动重启。我可以想象这会导致你的问题。尝试OnStartCommand()并让它返回&#34; START_NOT_STICKY&#34;旗。祝你好运!

答案 1 :(得分:1)

请参阅AlikElzin-kilaka here以下链接的答案。

顺便说一下,在你的stopService函数中使用你已经创建的mIntent并且不要创建一个新的Intent。其次,既然你也在使用stratService,你也可以使用stopSelf函数。

祝你好运!

答案 2 :(得分:1)

保存您正在创建的Intent。问题出在这里:

stopService(new Intent(MainActivity2.this, MyService.class));

在这里,您可以创建一个新的服务意图。但是你必须使用相同的意图,就像在startService方法

中一样