我正在启动一项服务,在运行时收听电话,效果很好。当我按下按钮停止服务时,它似乎停止,但仍然有效。我甚至不会在任务管理器中看到它。我用stopSelf()和onDestroy()等搜索并尝试了不同的东西,但没有运气......
package org.example.mystuff;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
public static boolean isRunning;
private final IBinder mBinder = new MyBinder();
private boolean mAllowRebind;
private int startId;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public void onCreate(){
isRunning = true;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
this.startId = startId;
return START_STICKY;
}
@Override
public boolean onUnbind(Intent intent) {
return mAllowRebind;
}
@Override
public void onRebind(Intent intent){
}
@Override
public void onDestroy(){
super.onDestroy();
stopSelf(startId);
isRunning = false;
}
}
以下是我在MainActivity中启动它的方法......
package org.example.mystuff;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Intent intent;
private TextView currentStatusTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(getBaseContext(), MyService.class);
currentStatusTV = (TextView) findViewById(R.id.currentStatusTextView);
checkServiceStatus();
}
@Override
public void onPause(){
super.onPause();
checkServiceStatus();
}
@Override
public void onResume(){
super.onResume();
checkServiceStatus();
}
@Override
public void onRestart(){
super.onRestart();
checkServiceStatus();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void startService(View view){
startService(intent);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
currentStatusTV.setText(R.string.service_running);
finish();
}
public void stopService(View view){
stopService(intent);
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
currentStatusTV.setText(R.string.service_stopped);
}
public void checkServiceStatus(){
if(MyService.isRunning){
currentStatusTV.setText(R.string.service_running);
} else {
currentStatusTV.setText(R.string.service_stopped);
}
}
}
我在MainActivity上有两个按钮,其中onClick属性调用startService和stopService,后者又调用服务的实际start / stopServices。 我再次尝试了许多其他解决方案,但似乎没有任何效果。我想知道我是否错过了其他重要的事情,因为我不熟悉服务。
答案 0 :(得分:0)
感谢beworker指出我正确的方向。我正在停止我的服务,但我的BroadcastReceiver仍然在我的AndroidManifest.xml中注册,我没有处理它。所以我使用PackageManager来启用和禁用MainActivity中的接收器。
要停止我将其添加到stopService():
ComponentName receiver = new ComponentName(this, CallReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
启用我在startService()中使用:PackageManager.COMPONENT_ENABLED_STATE_ENABLED
。