gpt4 book ai didi

安卓服务不工作

转载 作者:行者123 更新时间:2023-11-29 20:27:45 24 4
gpt4 key购买 nike

预期输出:单击 start_service 按钮后,将向 www.google.com 发送一个请求,根据请求是否成功,应显示带有适当消息的 AlertDialog。

当前输出: 第一次点击 start_service 按钮后,它会显示一个 AlertDialog“NO INTERNET ACCESS”。然而,从第二次点击开始,它会显示一个 AlertDialog“互联网”。

我做错了什么?

服务等级

public class RealService extends Service{

private static final String TAG="RealService";
private boolean isRunning=false;
private IBinder mBinder=new MyBinder();
private boolean intenetAccess=false;

Context context=this;
private RequestQueue reQueue=null;;

private final String url="http://www.google.com";

public boolean SendRequest()
{
reQueue=Volley.newRequestQueue(this);
StringRequest request=new StringRequest(com.android.volley.Request.Method.GET,
url,
new Response.Listener<String>() {

@Override
public void onResponse(
String response) {


intenetAccess=true;
Log.i(TAG,"intenetAccess=true");
}
},

new Response.ErrorListener() {

@Override
public void onErrorResponse(
VolleyError error) {

intenetAccess=false;

}
});

try{
reQueue.add(request);
}
catch(Exception e){}

return intenetAccess;

}

@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service onCreate");

isRunning=true;

}



@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "Service onBind");
return mBinder;
}

@Override
public void onRebind(Intent intent) {
Log.i(TAG, "Service onRebind");
super.onRebind(intent);
}

@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "Service onUnBind");
return true;
}

@Override
public void onDestroy() {

isRunning=false;
intenetAccess=false;
Log.i(TAG, "Service onDestroy");
super.onDestroy();
}



public class MyBinder extends Binder
{
RealService getService()
{
return RealService.this;
}
}
}

主 Activity 类

public class MainActivity extends AppCompatActivity{

private Button checkbtn;
private Button start_service_btn;
private Button stop_service_btn;

RealService realService=new RealService();
boolean serviceBound=false;
boolean internetPresent=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

start_service_btn=(Button) findViewById(R.id.start_service_btn);

start_service_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
internetPresent= realService.SendRequest();
if(internetPresent)
{
showAlertDialog(MainActivity.this, "INTERNET ACCESS", "You have internet access");

}
else
{
showAlertDialog(MainActivity.this, "NO INTERNET ACCESS", "You do not have internet access");

}
}
});

stop_service_btn=(Button) findViewById(R.id.stop_service_btn);

stop_service_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,RealService.class);
stopService(intent);
internetPresent=false;
}
});


public void showAlertDialog(Context context, String title,String message) {

AlertDialog alertDialog=new AlertDialog.Builder(context).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);

alertDialog.show();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onStart() {
super.onStart();
Intent intent=new Intent(this,RealService.class);
startService(intent);
bindService(intent, realServiceConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
super.onStop();
if(serviceBound)
{
unbindService(realServiceConnection);
serviceBound=false;
}
}

private ServiceConnection realServiceConnection=new ServiceConnection()
{

@Override
public void onServiceDisconnected(ComponentName name) {

serviceBound=false;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {

MyBinder myBinder=(MyBinder) service;
realService=myBinder.getService();
serviceBound=true;
}
};

}

最佳答案

您正在返回 bool 值:

return intenetAccess;

在请求完成之前。当您的 onResponse 监听器被调用并将 bool 值更改为 true 时,您已经返回了默认值 false。

那么你能做些什么来解决这个问题呢?一种方法是将监听器传递给 realService.SendRequest,并从您的 onResponse 和 onErrorResponse 触发监听器。

关于安卓服务不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32333770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com