下面列出了android.content.Context#stopService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (TextUtils.equals(intent.getAction(), JPushInterface.ACTION_MESSAGE_RECEIVED)) {
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
if (!TextUtils.isEmpty(message)) {
try {
if (!JSONObject.NULL.equals(new JSONObject(message))) {
context.stopService(new Intent(context, LetvPushService.class));
Intent serviceIntent = new Intent(context, LetvPushService.class);
serviceIntent.putExtra(JPushInterface.EXTRA_MESSAGE, message);
context.startService(serviceIntent);
if (!TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_MSG_ID))) {
JPushInterface.reportNotificationOpened(context, bundle.getString(JPushInterface.EXTRA_MSG_ID));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testBindStartStopUnbind() throws InterruptedException {
Context context = InstrumentationRegistry.getTargetContext();
ServiceConnection connection = bindToService();
awaitAndAssertEvents(ON_CREATE, ON_START);
context.startService(mServiceIntent);
// Precaution: give a chance to dispatch events
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
// still the same events
awaitAndAssertEvents(ON_CREATE, ON_START);
context.stopService(mServiceIntent);
// Precaution: give a chance to dispatch events
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
// service is still bound
awaitAndAssertEvents(ON_CREATE, ON_START);
context.unbindService(connection);
awaitAndAssertEvents(ON_CREATE, ON_START,
ON_STOP, ON_DESTROY);
}
/**
* 停止某个插件Service, 当全部的插件Service都停止之后, ProxyService也会停止
* @param targetIntent
* @return
*/
public int stopService(Intent targetIntent) {
ServiceInfo serviceInfo = selectPluginService(targetIntent);
if (serviceInfo == null) {
Log.w(TAG, "can not found service: " + targetIntent.getComponent());
return 0;
}
Service service = mServiceMap.get(serviceInfo.name);
if (service == null) {
Log.w(TAG, "can not runnning, are you stopped it multi-times?");
return 0;
}
service.onDestroy();
mServiceMap.remove(serviceInfo.name);
if (mServiceMap.isEmpty()) {
// 没有Service了, 这个没有必要存在了
Log.d(TAG, "service all stopped, stop proxy");
Context appContext = UPFApplication.getContext();
appContext.stopService(new Intent().setComponent(new ComponentName(appContext.getPackageName(), ProxyService.class.getName())));
}
return 1;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_SHUTDOWN)) {
android.util.Log.d(
"Linphone",
"[Boot Receiver] Device is shutting down, destroying Core to unregister");
context.stopService(
new Intent(Intent.ACTION_MAIN).setClass(context, LinphoneService.class));
} else if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
LinphonePreferences.instance().setContext(context);
boolean autostart = LinphonePreferences.instance().isAutoStartEnabled();
android.util.Log.i(
"Linphone", "[Boot Receiver] Device is starting, auto_start is " + autostart);
if (autostart && !LinphoneService.isReady()) {
startService(context);
}
} else if (intent.getAction().equalsIgnoreCase(Intent.ACTION_MY_PACKAGE_REPLACED)) {
LinphonePreferences.instance().setContext(context);
boolean foregroundService =
LinphonePreferences.instance().getServiceNotificationVisibility();
android.util.Log.i(
"Linphone",
"[Boot Receiver] App has been updated, foreground service is "
+ foregroundService);
if (foregroundService && !LinphoneService.isReady()) {
startService(context);
}
}
}
static void stopAutoRegistration(Context context, boolean clearRegistration) {
//PPApplication.logE("PhoneStateScanner.stopAutoRegistration", "xxx");
// stop registration service
context.stopService(new Intent(context.getApplicationContext(), MobileCellsRegistrationService.class));
//MobileCellsRegistrationService.stop(context);
if (clearRegistration) {
//clearEventList();
// set enabledAutoRegistration=false
//PPApplication.logE("[REG] PhoneStateScanner.stopAutoRegistration", "setMobileCellsAutoRegistration(true)");
MobileCellsRegistrationService.setMobileCellsAutoRegistration(context, true);
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_REASON);
if (reason != null) {
if (reason.equals(SYSTEM_HOME_KEY)) {
Log.e("homekey", "home键被点击");
context.stopService(new Intent(context, DrawService.class));
} else if (reason.equals(SYSTEM_RECENT_APPS)) {
Log.e("homekey", "长按home键");
}
}
}
}
/**
* 停止某个插件Service, 当全部的插件Service都停止之后, ProxyService也会停止
*
* @param targetIntent targetIntent
* @return int
*/
public int stopService(Intent targetIntent) {
ServiceInfo serviceInfo = selectPluginService(targetIntent);
if (serviceInfo == null) {
Log.w(TAG, "can not found service: " + targetIntent.getComponent());
return 0;
}
String processName3 = ProcessUtil.getProcessNameViaManager(MApplication.getInstance());
Log.d(TAG, "3processName:" + processName3);
Log.d(TAG, "3mServiceMap.size:" + mServiceMap.size());
Log.d(TAG, "3mServiceMap.get(serviceInfo.name):" + serviceInfo.name);
Service service = mServiceMap.get(serviceInfo.name);
if (service == null) {
Log.w(TAG, "can not runnning, are you stopped it multi-times?");
return 0;
}
service.onDestroy();
mServiceMap.remove(serviceInfo.name);
if (mServiceMap.isEmpty()) {
// 没有Service了, 这个没有必要存在了
Log.d(TAG, "service all stopped, stop proxy");
Context appContext = MApplication.getInstance();
appContext.stopService(new Intent().setComponent(new ComponentName(appContext.getPackageName(), PluginProxyService.class.getName())));
}
return 1;
}
public static void closePipView(Context context) {
if (context != null) {
Intent serviceStop = new Intent();
serviceStop.setClass(context, DialogService.class);
context.stopService(serviceStop);
}
}
public static void startOrStopCrazyLogger(Context context) {
boolean alreadyRunning = checkIfServiceIsRunning(context, CrazyLoggerService.class);
Intent intent = new Intent(context, CrazyLoggerService.class);
if (!alreadyRunning) {
context.startService(intent);
} else {
context.stopService(intent);
}
}
public static synchronized void stopBackgroundServiceIfRunning(Context context) {
boolean alreadyRunning = ServiceHelper.checkIfServiceIsRunning(context, LogcatRecordingService.class);
log.d("Is CatlogService running: %s", alreadyRunning);
if (alreadyRunning) {
Intent intent = new Intent(context, LogcatRecordingService.class);
context.stopService(intent);
}
}
@Override
public void onReceive(Context context, Intent intent) {
Intent discoveryIntent = new Intent(context, UrlDeviceDiscoveryService.class);
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
context.startService(discoveryIntent);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
context.stopService(discoveryIntent);
}
}
public static void autoStopWakelockService(Context c){
if(isWakeLockServiceRunning()){
c.stopService(new Intent(c, WakeLockService.class));
}
}
public static void stopService(Context context) {
Intent intent = new Intent(context, ReleaseService.class);
context.stopService(intent);
// 用户想要关闭悬浮窗
PreferenceHelper.setIsRunning(context, false);
}
@Override
public void onReceive(Context context, Intent intent) {
Intent uploadIntent = new Intent(context, UploadService.class);
context.stopService(uploadIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
Intent syncIntent = new Intent(context, SyncService.class);
context.stopService(syncIntent);
}
public static void stopServer(Context context) {
Intent intent = new Intent(context, SocketService.class);
context.stopService(intent);
}
public static void intentToStop(Context context) {
context.stopService(newIntent(context));
}
public static void deactivateService(Context context) {
context.startService(getServiceIntent(context).setAction(DaedalusVpnService.ACTION_DEACTIVATE));
context.stopService(getServiceIntent(context));
}
public static void stopService(Context context) {
Intent serviceIntent = new Intent(context, XMPPSessionService.class);
serviceIntent.setPackage("com.nanoscopia.services");
context.stopService(serviceIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
Intent shutdown = new Intent(context, MockWalkerService.class);
context.stopService(shutdown);
}