package com.anyun.exam.lib; import android.app.Service; import android.content.Intent; import android.content.SharedPreferences; import android.os.IBinder; import android.os.RemoteCallbackList; import android.os.RemoteException; import android.util.Base64; import android.util.Log; import com.anyun.exam.lib.util.DESUtil; import com.anyun.exam.lib.util.NetUtils; import com.anyun.exam.lib.util.Speaker; import androidx.annotation.Nullable; import java.util.concurrent.atomic.AtomicBoolean; /** * MyApplication2 * Created by lzw on 2019/6/5. 13:22:46 * 邮箱:632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class RemoteService extends Service { private static final String TAG = "RemoteService"; private final static String LOAD_KEY = "RTKBaseStation"; /**服务是否销毁标志*/ private AtomicBoolean mIsServiceDestroyed = new AtomicBoolean(false); private RemoteCallbackList mListenerList = new RemoteCallbackList(); private IListenerInterface mListener; private int msgId = 0; private Speaker speaker = null; private IRemoteInterface.Stub iRemoteInterface = new IRemoteInterface.Stub(){ @Override public void registListener(IListenerInterface i) throws RemoteException { //注册回调方法 mListenerList.register(i); } @Override public void unRegistListener(IListenerInterface i) throws RemoteException { mListenerList.unregister(i); } @Override public void add() throws RemoteException { Log.i(TAG,"客户端调用服务端方法"); } }; @Nullable @Override public IBinder onBind(Intent intent) { return iRemoteInterface; } @Override public void onCreate() { super.onCreate(); Log.i(TAG,"onCreate()"); speaker = new Speaker(getApplicationContext()); startNative(); new Thread(new Worker()).start(); } private class Worker implements Runnable{ @Override public void run() { while (!mIsServiceDestroyed.get()){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } onMessageArrived(String.format("消息ID:%d,请注意查收",msgId++)); } } } @Override public void onDestroy() { super.onDestroy(); Log.i(TAG,"onDestroy"); mIsServiceDestroyed.set(true); } private void onMessageArrived(String json){ int N = mListenerList.getRegisteredCallbackCount(); Log.d(TAG, "getRegisteredCallbackCount = " + N); mListenerList.beginBroadcast(); for (int i = 0; i < N; i++) { mListener = mListenerList.getBroadcastItem(i); if (mListener != null){ try { mListener.onMessageArrived(json); } catch (RemoteException e) { e.printStackTrace(); } } } mListenerList.finishBroadcast(); } public String javaGetImei() { return NetUtils.getDeviceId(getApplicationContext()); } public String javaDESEncrypt(String plaintext, String key) { try { byte []des = DESUtil.encrypt(plaintext.getBytes("utf-8"), key); return Base64.encodeToString(des, Base64.DEFAULT); } catch (Exception e) { } return null; } public byte[] javaDESEncrypt(byte []plaintext, byte []key) { try { return DESUtil.encrypt(plaintext, key); } catch (Exception e) { } return null; } public void SetPlatformKey(byte []key) { String params = Base64.encodeToString(key, Base64.DEFAULT); SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("platform_key", params); editor.commit(); } public byte[] GetPlatformKey() { SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); String params = sharedPreferences.getString("platform_key", ""); if (params.equals("")) { return null; } return Base64.decode(params, Base64.DEFAULT); } public void DeletePlatformKey() { SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("platform_key", ""); editor.commit(); } public void SetSharedValue(String key, int value) { SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(key, value); editor.commit(); } public int GetSharedValue(String key) { SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); return sharedPreferences.getInt(key, 0); } public void TextSpeak(String text) { if (speaker != null) { speaker.speak(text); } } // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); } public native void startNative(); }