Commit 6c894bdb by System Administrator

更新develop分支代码

parent b5391f55
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,12 +17,15 @@ import org.apache.cordova.CallbackContext; ...@@ -15,12 +17,15 @@ 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.
*/ */
public class ONService extends CordovaPlugin { public class ONService extends CordovaPlugin {
private static final String TAG = "ONService"; private static final String TAG = "ONService";
private NotificationService.MyBinder myBinder; private NotificationService.MyBinder myBinder;
// private IRemoteService myBinder;
private ServiceConnection connection = new ServiceConnection() { private ServiceConnection connection = new ServiceConnection() {
@Override @Override
...@@ -28,6 +33,7 @@ public class ONService extends CordovaPlugin { ...@@ -28,6 +33,7 @@ public class ONService extends CordovaPlugin {
NotificationService.webView = webView; NotificationService.webView = webView;
NotificationService.cordovaPlugin = cordova; NotificationService.cordovaPlugin = cordova;
myBinder = (NotificationService.MyBinder) iBinder; myBinder = (NotificationService.MyBinder) iBinder;
// myBinder = IRemoteService.Stub.asInterface(iBinder);
// myBinder.startForeGround(); // myBinder.startForeGround();
} }
...@@ -61,19 +67,33 @@ public class ONService extends CordovaPlugin { ...@@ -61,19 +67,33 @@ public class ONService extends CordovaPlugin {
// 开启并绑定服务并5秒间隔调取getHistory() // 开启并绑定服务并5秒间隔调取getHistory()
if (action.equals("bindService")) { if (action.equals("bindService")) {
Intent intent = new Intent(cordova.getActivity(), NotificationService.class); Intent intent = new Intent(cordova.getActivity(), NotificationService.class);
try { if (isServiceExisted(NotificationService.class.getName())) {
intent.putExtra("idCard", args.getString(0)); Log.e(TAG, "execute: 前台服务已存在");
intent.putExtra("schoolId", args.getString(1)); cordova.getActivity().bindService(
} catch (JSONException e) { intent,
e.printStackTrace(); connection,
Service.BIND_AUTO_CREATE
);
} else {
try {
intent.putExtra("idCard", args.getString(0));
intent.putExtra("schoolId", args.getString(1));
} catch (JSONException e) {
e.printStackTrace();
}
try {
cordova.getActivity().startForegroundService(intent);
Log.e(TAG, "startForeGround");
} catch (Error e) {
cordova.getActivity().startService(intent);
Log.e(TAG, "startService");
}
cordova.getActivity().bindService(
intent,
connection,
Service.BIND_AUTO_CREATE
);
} }
// webView.loadUrl("http://localhost/tabs/tab3");
cordova.getActivity().startService(intent);
cordova.getActivity().bindService(
intent,
connection,
Service.BIND_AUTO_CREATE
);
callbackContext.success("服务开启成功"); callbackContext.success("服务开启成功");
return true; return true;
} }
...@@ -81,20 +101,36 @@ public class ONService extends CordovaPlugin { ...@@ -81,20 +101,36 @@ public class ONService extends CordovaPlugin {
// 开始间隔调取getHistory() // 开始间隔调取getHistory()
if (action.equals("startLoop")) { if (action.equals("startLoop")) {
if (myBinder != null) { if (myBinder != null) {
myBinder.startForeGround(); // myBinder.startForeGround();
} }
} }
// 接触activity与service的绑定(最好在退出时调用) // 接触activity与service的绑定(最好在退出时调用)
if (action.equals("unbindService")) { if (action.equals("stopService")) {
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
cordova.getActivity().unbindService(connection); cordova.getActivity().unbindService(connection);
cordova.getActivity().stopService(new Intent(cordova.getActivity(), NotificationService.class));
} }
}); });
} }
// 接触activity与service的绑定(最好在退出时调用)
if (action.equals("unbindService")) {
if (isServiceExisted(NotificationService.class.getName())){
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
cordova.getActivity().unbindService(connection);
}
});
}
}
return false; return false;
} }
private void coolMethod(String message, CallbackContext callbackContext) { private void coolMethod(String message, CallbackContext callbackContext) {
...@@ -104,4 +140,17 @@ public class ONService extends CordovaPlugin { ...@@ -104,4 +140,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;
}
}
\ No newline at end of file
...@@ -21,7 +21,6 @@ import android.support.annotation.RequiresApi; ...@@ -21,7 +21,6 @@ import android.support.annotation.RequiresApi;
import android.util.Log; import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.orhon.SmartCloudTest.IRemoteService;
import com.orhon.SmartCloudTest.MainActivity; import com.orhon.SmartCloudTest.MainActivity;
import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaInterface;
......
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