I have a thread class:
private class Mythread extends Thread{
boolean mfinish = false;
private void finish(){
mfinish = true;
interrupt();
}
while(!mfinish){ do my work}
}
and I use SparseArray<Mythread> mThreadArray
to hold threads.
I registered a BroadcastReceiver
in my service A:
private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
if(intent.getAction().euqals(STOP_THREAD)){
uid = bundle.getInt("uid");
pid = bundle.getInt("pid");
tid = bundle.getInt("tid");
if(mThreadArray.get(tid)==null){
Mythread samplThread = new Mythread(pid, tid);
mThreadArray.put(tid,sampleMainThread);
sampleThread.start();
}
}
if(intent.getAction().equals(STOP_THREAD)){
uid = bundle.getInt("uid");
pid = bundle.getInt("pid");
tid = bundle.getInt("tid");
if(mThreadArray.get(tid)!=null){
mThreadArray.get(tid).finish();
mThreadArray.remove(tid);
}
}
}
}
In another application B, sentBroadcast()
when there is a thread is created or ended.
However, in my service A, I found the created threads can't be stopped when the "stop" intent comes.
Does any body know the reason?