I run the code and get the following result, but I hope the application can work in the order "A" → "OnDestroy Service" → "B" → "C", how can I do it?
In My path 2 section, I try to put the code in a function new Handler().postDelayed(new Runnable() {}, this is normal, it worked in the order "A" → "OnDestroy Service" → "B" → "C", I don’t know why the path can be successful, I don’t know is this a good way!
Result
11-13 10: 04: 32.137 27947-27947 / info.dodata.screenrecorder E / My: A
11-13 10: 04: 32.147 27947-27947 / info.dodata.screenrecorder E / My: B
11-13 10: 04: 32.157 27947-27947 / info.dodata.screenrecorder E / My: C
11-13 10: 04: 32.157 27947-27947 / info.dodata.screenrecorder E / My: Service OnDestroy
UIAbou.cs
public class UIAbout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about);
Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
startService(intent1);
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("My", "A");
Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
stopService(intent1);
Log.e("My", "B");
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
Log.e("My", "C");
}
});
}
}
RecordService.cs
public class RecordService extends Service {
private Context mContext;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
}
@Override
public void onDestroy(){
Log.e("My","Service OnDestroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
}
}
======================== My path 1 ========================= ================
I set the isServiceStoped label to track the Stop Service completion, but my application freezes after requesting the result "11-13 11: 31: 23.107 7599-7599 / info.dodata.screenrecorder E / My: A"
New UIAbout.cs
public class UIAbout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about);
Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
startService(intent1);
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("My", "A");
Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
stopService(intent1);
while (RecordService.isServiceStoped==false){
}
Log.e("My", "B");
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
Log.e("My", "C");
}
});
}
}
New RecordService.cs
public class RecordService extends Service {
public static boolean isServiceStoped=true;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
}
@Override
public void onDestroy(){
Log.e("My", "Service OnDestroy");
isServiceStoped=true;
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
isServiceStoped=false;
return super.onStartCommand(intent,flags,startId);
}
}
====================== My path 2 =========================== ==================
I try to put the code in a function new Handler().postDelayed(new Runnable() {}, this is normal, it worked in the order "A" → "OnDestroy Service" → "B" → "C",
I don’t know why the path can be successful, I don’t know if the way is good.
Latest UIAbout.cs
public class UIAbout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about);
Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
startService(intent1);
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("My", "A");
Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
stopService(intent1);
new Handler().postDelayed(new Runnable() {
public void run() {
Log.e("My", "B");
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
Log.e("My", "C");
}
}, 1);
}
});
}
}
Latest RecordService.cs
public class RecordService extends Service {
private Context mContext;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
}
@Override
public void onDestroy(){
Log.e("My", "Service OnDestroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
}
}