我運行下面的後臺服務:無法停止後臺服務的Android
public class TranslationService extends IntentService implements ClipboardManager.OnPrimaryClipChangedListener {
private Language mFrom;
private Language mTo;
private String mTranslation;
private ClipboardManager mClipboardManager;
public TranslationService() {
super("hello");
}
public TranslationService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mFrom = (Language) intent.getSerializableExtra(MainActivity.FROM_STRING);
mTo = (Language) intent.getSerializableExtra(MainActivity.TO_STRING);
mClipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
mClipboardManager.addPrimaryClipChangedListener(this);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onPrimaryClipChanged() {
ClipData clip = mClipboardManager.getPrimaryClip();
String copiedText = clip.getItemAt(0).getText().toString();
connectToTranslationApi(copiedText);
//TODO solve concurrency problems if app works inconsistently.
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(), mTranslation, Toast.LENGTH_SHORT).show();
}
});
}
private void connectToTranslationApi(String copiedPhrase) {
String apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
String fromInitials = mFrom.initials;
String toInitials = mTo.initials;
String yandexUrl = "https://translate.yandex.net/api/v1.5/tr.json/translate?key="+
apiKey + "&lang=" +
fromInitials + "-" + toInitials +
"&text="+ copiedPhrase;
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(yandexUrl).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
//TODO handle failure
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String jsonData = response.body().string();
try {mTranslation = getTextTranslation(jsonData);}
catch (JSONException e) {e.printStackTrace();}
}
}
});
}
private String getTextTranslation(String jsonData) throws JSONException {
JSONObject data = new JSONObject(jsonData);
String translation = data.getString("text");
translation = translation.replace("]", "")
.replace("[", "")
.replace("\"", "");
return translation;
}
}
該服務在後臺運行,並翻譯,我複製到剪貼板(甚至當我使用其他應用程序文本服務只會停止,如果我刪除從RAM啓動它的應用程序),這一切都很好。
但是,當我回到應用程序的主要活動並按下「停止服務」它不會停止。這裏是開始(並應該停止)服務的代碼
if(mIsServiceRunning == false) {
Intent i = new Intent(MainActivity.this, TranslationService.class);
i.putExtra(FROM_STRING, mLanguageFrom);
i.putExtra(TO_STRING, mLanguageTo);
startService(i);
mIsServiceRunning = true;
startButton.setText("Stop Service");
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
}
else {
Intent i = new Intent(MainActivity.this, TranslationService.class);
stopService(i);
mIsServiceRunning = false;
startButton.setText("Start Service");
}
任何想法如何阻止它?
你怎麼知道它不停止? – JozeRi