0
我有開始這樣的一個服務的活動:安卓:在停止服務奇怪的問題
Intent youtubeIntent = new Intent(this, YoutubeFeedService.class);
service = startService(youtubeIntent);
並在服務站我用廣播接收器來檢測:
@Override
public void onResume() {
IntentFilter filter;
filter = new IntentFilter(YoutubeFeedService.NEW_VIDEO_CELL);
receiver = new FeaturedReceiver();
registerReceiver(receiver, filter);
super.onResume();
}
@Override public void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
public class FeaturedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String title = intent.getStringExtra("title");
if (title.equals("-1") || title.equals("1")){
//stopService(new Intent(FeaturedActivity.this, service.getClass()));
try {
Class serviceClass = Class.forName(service.getClassName());
stopService(new Intent(FeaturedActivity.this, serviceClass));
}
catch (ClassNotFoundException e) {}
}
}
}
我第一次試圖殺死
stopService(new Intent(FeaturedActivity.this, service.getClass()));
服務,但這並沒有工作,所以我代替使用
try {
Class serviceClass = Class.forName(service.getClassName());
stopService(new Intent(FeaturedActivity.this, serviceClass));
}
catch (ClassNotFoundException e) {}
而且它的工作!任何人都可以解釋有什麼區別?
由於