赞
踩
- package com.asus.cnfindphone.service.persistentdata;
-
- // Declare any non-default types here with import statements
-
- interface IDataBlockService {
- byte[] read();
- int write(in byte[] data);
- void wipe();
- }
- package com.asus.cnfindphone.service.persistentdata;
-
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.util.Log;
-
- import android.service.persistentdata.PersistentDataBlockManager;
-
- /**
- * Created by Qinghua_Liu on 2017-2-28.
- */
- public class DataBlockService extends Service {
- private static String TAG = "Qinghua";
-
- @Override
- public void onCreate() {
- super.onCreate();
- }
-
- private boolean bInPSTList(String calling) {
- boolean bRes = false;
- if (calling.equalsIgnoreCase("asus.findmyphone")) {
- bRes = true;
- }
- Log.d(TAG, "bInPSTList==" + bRes);
- return bRes;
- }
-
- @Override
- public IBinder onBind(Intent intent) {
- //String callingApp = intent.getComponent().getPackageName();
- return mBind;
- }
-
- private final IDataBlockService.Stub mBind = new IDataBlockService.Stub() {
- @Override
- public byte[] read() throws RemoteException {
- byte[] bytes = null;
- Log.d(TAG, "DataBlockService.read()");
- String callingApp = getApplicationContext().getPackageManager().getNameForUid(Binder.getCallingUid());
- Log.d(TAG, "callingApp==" + callingApp);
- if (bInPSTList(callingApp)) {
- PersistentDataBlockManager pdbManager = (PersistentDataBlockManager) getApplicationContext().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
- bytes = pdbManager.read();
- //String id ="shenshiid";
- //return id.getBytes();
- }
- Log.d(TAG, "pdbManager.read()==" + new String(bytes));
- return bytes;
- }
-
- @Override
- public int write(byte[] data) throws RemoteException {
- Log.d(TAG, "DataBlockService.write()" + new String(data));
- String callingApp = getApplicationContext().getPackageManager().getNameForUid(Binder.getCallingUid());
- Log.d(TAG, "callingApp==" + callingApp);
- if (bInPSTList(callingApp)) {
- if (data != null && data.length > 0) {
- PersistentDataBlockManager pdbManager = (PersistentDataBlockManager) getApplicationContext().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
- pdbManager.write(data);
- }
- }
- return 0;
- }
-
- @Override
- public void wipe() throws RemoteException {
-
- }
- };
- }
- @Override
- public byte[] read() throws RemoteException {
- byte[] bytes = null;
- Log.d(TAG, "DataBlockService.read()");
- String callingApp = getApplicationContext().getPackageManager().getNameForUid(Binder.getCallingUid());
- Log.d(TAG, "callingApp==" + callingApp);
- if (bInPSTList(callingApp)) {
- PersistentDataBlockManager pdbManager = (PersistentDataBlockManager) getApplicationContext().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
- bytes = pdbManager.read();
- //String id ="shenshiid";
- //return id.getBytes();
- }
- Log.d(TAG, "pdbManager.read()==" + new String(bytes));
- return bytes;
- }
- private boolean bInPSTList(String calling) {
- boolean bRes = false;
- if (calling.equalsIgnoreCase("asus.findmyphone")) {
- bRes = true;
- }
- Log.d(TAG, "bInPSTList==" + bRes);
- return bRes;
- }
- import com.asus.cnfindphone.service.persistentdata.IDataBlockService;
-
- private IDataBlockService dataBlockService;
- private ServiceConnection conn;
- private void initService(){
- conn = new ServiceConnection() {
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- Log.d("Qinghua","onServiceDisconnected! ");
- }
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- dataBlockService = IDataBlockService.Stub.asInterface((IBinder)service);
- Log.d("Qinghua","bind success! ");
- }
- };
- Intent i = new Intent("com.asus.cnfindphone.service.persistentdata.DataBlockService");
- i.setPackage("com.asus.cnfindphone");
- bindService(i, conn, Context.BIND_AUTO_CREATE);
- }
- protected void onCreate(Bundle paramBundle) {
- super.onCreate(paramBundle);
- setContentView(R.layout.activity_main);
- initService();
- this.location.setOnClickListener(new View.OnClickListener() {
- public void onClick(View paramView) {
- try {
- dataBlockService.write("cissy".getBytes());
- }catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- });
- this.test.setOnClickListener(new View.OnClickListener() {
- public void onClick(View paramView) {
- try {
- byte[] bytes = dataBlockService.read();
- Log.d("Qinghua","result:"+new String(bytes));
- }catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- });
-
- }
- @Override protected void onDestroy()
- {
- super.onDestroy();
- unbindService(conn);
- dataBlockService = null;
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。