Reputation: 281
I called the stopService() method from the calling class when the calling activity exits, and the service does not stop. It keeps on running in the background and can thus be a hastle to the end user. Any help would be greatly appreciated..
Code:
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();
}
});
}
MY SERVICE CLASS:
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();
}
}
Upvotes: 0
Views: 635
Reputation: 14022
Docs say:
Each timer has one thread on which tasks are executed sequentially.
So I see you have a thread in your service.If you created a thread,you can not stop it until it does it's task,unless it has a task that involves a loop.In this situation,in loop condition,you can check if this thread received stop message(cancel) or not?And if is received,stop the loop.
Upvotes: 1
Reputation: 281
Note to self:
Bro, put System.exit(0) in onDestroy() , after timer.cancel() & timer.purge()
Upvotes: 0