Android 当线程处于脱机状态时,如何停止正在使用的线程?
我正在开发一款android应用程序,当它通过使用服务和广播接收器上网时,我会调用webservice。下面是我的代码:Android 当线程处于脱机状态时,如何停止正在使用的线程?,android,android-intentservice,Android,Android Intentservice,我正在开发一款android应用程序,当它通过使用服务和广播接收器上网时,我会调用webservice。下面是我的代码: public class YourService extends IntentService { private static String TAG = YourService.class.getSimpleName(); private MyThread mythread; public boolean isRunning = false; JSONArray SaveOr
public class YourService extends IntentService {
private static String TAG = YourService.class.getSimpleName();
private MyThread mythread;
public boolean isRunning = false;
JSONArray SaveOrderDart,SaveOrderDetails,UpdateDart,updateOrderDetails;
private static String urlDartorder= Config.url+"SaveDartDetails";
private static String urlupdateOrderDetails= Config.url+"UpdateOrderDetails";
private static String urlSaveOrderDetails= Config.url+"SaveOrderDetails";
private static String urlDartupdate= Config.url+"UpdateDartDetails";
DartDatabase ddb;
OrderDatabase ord;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public YourService(String name) {
super(name);
}
@Override
public void onCreate() {
super.onCreate();
mythread=new MyThread();
}
public YourService()
{
super("call webservice");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Bundle extras = intent.getExtras();
boolean isNetworkConnected = extras.getBoolean("isNetworkConnected");
// your code
if(isNetworkConnected){
if(!isRunning){
mythread.start();
isRunning = true;
}
Log.e("TAG", "Yes");
}
else {
if (mythread != null) {
isRunning = false;
mythread.interrupt();
}
//mythread.stop();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(!isRunning){
mythread.start();
isRunning = true;
}
}
我的广播接收机
public class ConnectivityChangeReceiver extends BroadcastReceiver {
boolean mLastState =false;
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that which service class will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
YourService.class.getName());
//mLastState = true;
intent.putExtra("isNetworkConnected", isConnected(context));
context.startService((intent.setComponent(comp)));
}
public boolean isConnected(Context context) {
ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
}
我的目的是当互联网存在时,我想调用webservice not present然后停止线程和服务,但根据此代码,我无法停止服务我不知道为什么请帮助我但我不能停止线程请帮助我 不需要在IntentDevice类的OnHandleContent内创建新线程。IntentService本身提供了一个单独的线程,用于执行长时间运行的操作。通过使用IntentService,应用程序开发人员无需“管理”线程。所以,不需要“手动”或通过代码(这是您所要求的)停止线程。阅读Android开发者网站上关于如何使用IntentService的IntentService文档,了解更多详细信息。我不想知道你想说什么