当调用活动退出时,我从调用类调用了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();
}
}
答案 0 :(得分:1)
Docs说:
每个计时器都有一个线程,顺序执行任务。
所以我看到你的服务中有一个线程。如果你创建了一个线程,除非它有一个涉及循环的任务,否则你无法阻止它,除非它有一个涉及循环的任务。在这种情况下,在循环条件下,你
是否可以检查此线程是否收到停止消息(取消)?如果收到,则停止循环。答案 1 :(得分:0)
自我注意:
Bro,在timer.cancel()&amp;之后将System.exit(0)放在onDestroy()中。 timer.purge()