[英]How to finish activity from service class in android?
我正在開發一個應用程序,我想從后台服務器獲取數據。我在后台使用service獲取數據。現在我想處理網絡異常,如果沒有網絡我想顯示一個警告對話框並完成應用程序。 這里我有問題,即服務類中的整理活動顯示異常ClassCastExcetption..Exception在這里提出(((Activity)mContext).finish())我嘗試了這么多,但我無法得到這個請任何人解決我的問題
public class InitialRequestService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
new InitialRequestData(InitialRequestService.this).execute();
}
public class InitialRequestData extends AsyncTask<String, Void, Void> {
public InitialRequestData(Context context) {
this.mContext = context;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
isNetworkAvailable = CheckNetWork.isConnectedToInternet(mContext);
}
@Override
protected Void doInBackground(String... params) {
//
}
@Override
protected void onPostExecute(FeatureResult result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result==null || isNetworkAvailable==false){
/*String message="Unable to connect to the server, please try again";
FieldsValdations.AlertErrorMessage(mContext, message, "Network failure");*/
final AlertDialog alert = new AlertDialog.Builder(mContext).create();
alert.setTitle("Network failure");
alert.setMessage("Unable to connect to the server, please try again");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
((Activity) mContext).finish();
}
});
alert.setCancelable(false);
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
}
}
}
}
排隊
((Activity) mContext).finish();
mContext
來自new InitialRequestData(InitialRequestService.this).execute();
它是InitialRequestService
,而不是Activity
,所以你得到一個ClassCastExcetption。
您需要將Activity實例傳遞給Service。 但我更喜歡在您的InitialRequestService
中將BroadcastReceiver
發送到這樣的Activity:
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
// modify here
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager
.getInstance(InitialRequestService.this);
localBroadcastManager.sendBroadcast(new Intent(
"com.durga.action.close"));
}
});
在要關閉的活動中:
public class YourActivity extends Activity{
LocalBroadcastManager mLocalBroadcastManager;
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("com.durga.action.close")){
finish();
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction("com.durga.action.close");
mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, mIntentFilter);
}
protected void onDestroy() {
super.onDestroy();
mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);
}
}
希望能幫助到你。
我認為你必須從服務中傳遞簡單的意圖 ,如下所示。
Intent myIntent = new Intent(First.this,Secound.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle myKillerBundle = new Bundle();
myKillerBundle.putInt("kill",1);
myIntent.putExtras(myKillerBundle);
getApplication().startActivity(myIntent);
然后在Secound.class中
onCreate(Bundle bundle){
if(this.getIntent().getExtras().getInt("kill")==1)
finish();
}
否則請使用BroadcastReceiver 。請參閱下面的示例。
希望它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.