通过SN控制设备

在frameworks/base/packages/SystemUI/AndroidManifest.xml注册service

<service android:name=".ControlServer"
    android:exported="false" >
</service>

然后再指定路径下新建文件frameworks/base/packages/SystemUI/assets/SecurityUrl.txt

新建frameworks/base/packages/SystemUI/src/com/android/systemui/ControlServer.java

package com.android.systemui;

import java.io.BufferedReader;
import java.util.Timer;
import java.util.TimerTask;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.Context;
import android.content.Intent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Build;
import android.os.SystemProperties;
import android.provider.Settings;
import android.util.Log;

public class ControlServer extends Service {
    private static final String TAG  = "MyControl";
    private String SecurityUrlFile = "SecurityUrl.txt";
    private List<String> listUrl = new ArrayList<String>();
    private int isPowerOff;
    private final static String ACTION_SHUTDOWN = "custom_shutdown";


    @Override
    public void onCreate() {
        super.onCreate();
        querySecurityUrl(SecurityUrlFile);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        querySecurityUrl(SecurityUrlFile);
        ControlDevices(getSerialNumber());
    }

    private String getSerialNumber() {
        Log.d(TAG, "getSerialNumber = "+ Build.SERIAL);
        return SystemProperties.get("ro.lenovosn2","unknown");
    }

    private void ControlDevices(String number) {
        Log.d(TAG, "ControlDevices = "+ number);
        isPowerOff = listUrl.size();
        for (int i = 0; i < listUrl.size(); i++) {
            if (listUrl.get(i).equals(number)) {
                isPowerOff--;
            }
        }
        if (isPowerOff >= listUrl.size()){
            //sendBroadcast(new Intent("custom_shutdown"));
            Log.i (TAG,"sendBroadcast(new Intent(custom_shutdown))");
       } else{
           Settings.Secure.putInt(getContentResolver(),"custom_control_alltable",1);
       }
    }

    private List<String> querySecurityUrl(String path) {
        try {
            InputStream is = getAssets().open(path);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String url = null;
            while ((url = br.readLine()) != null) {
                listUrl.add(url);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return listUrl;
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

在frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java中判断是否启动service

if(0 ==Settings.Secure.getInt(context.getContentResolver(),"custom_control_alltable",0)){
    Intent serIntent = new Intent(contextcom.android.systemui.ControlServer.class);
    serIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startService(serIntent);
}

猜你喜欢

转载自blog.csdn.net/lancelots/article/details/82149252