Commit 9f390881 by TakeItEasyJQ

提交代码

parent 56700e47
package com.orhon.notification; package com.orhon.notification;
import android.app.ActivityManager;
import android.app.Service; import android.app.Service;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
...@@ -15,6 +17,9 @@ import org.apache.cordova.CallbackContext; ...@@ -15,6 +17,9 @@ import org.apache.cordova.CallbackContext;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import java.util.List;
/** /**
* This class echoes a string called from JavaScript. * This class echoes a string called from JavaScript.
*/ */
...@@ -87,12 +92,14 @@ public class ONService extends CordovaPlugin { ...@@ -87,12 +92,14 @@ public class ONService extends CordovaPlugin {
// 接触activity与service的绑定(最好在退出时调用) // 接触activity与service的绑定(最好在退出时调用)
if (action.equals("unbindService")) { if (action.equals("unbindService")) {
cordova.getActivity().runOnUiThread(new Runnable() { if (isServiceExisted(NotificationService.class.getName())){
@Override cordova.getActivity().runOnUiThread(new Runnable() {
public void run() { @Override
cordova.getActivity().unbindService(connection); public void run() {
} cordova.getActivity().unbindService(connection);
}); }
});
}
} }
return false; return false;
} }
...@@ -104,4 +111,17 @@ public class ONService extends CordovaPlugin { ...@@ -104,4 +111,17 @@ public class ONService extends CordovaPlugin {
callbackContext.error("Expected one non-empty string argument."); callbackContext.error("Expected one non-empty string argument.");
} }
} }
public boolean isServiceExisted(String className) {
ActivityManager am = (ActivityManager) cordova.getActivity().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = am.getRunningServices(Integer.MAX_VALUE);
int myUid = android.os.Process.myUid();
for (ActivityManager.RunningServiceInfo runningServiceInfo : serviceList) {
if (runningServiceInfo.uid == myUid && runningServiceInfo.service.getClassName().equals(className)) {
return true;
}
}
return false;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment