Android修改记录《一》

启动其他Activity

private void startOtherActivity(Context context){

      Intent i = new Intent(); 

        ComponentName comp = 

            new ComponentName("packageName","PackageName+ActivityName"); 

        i.setComponent(comp); 

        i.setAction("android.intent.action.MAIN"); 

        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

        context.startActivity(i);

    }


打开gps

需要添加权限,并且是系统apk

android.permission.WRITE_SECURE_SETTINGS

Settings.Secure.setLocationProviderEnabled(context.getContentResolver(), LocationManager.GPS_PROVIDER, true);

Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,        android.provider.Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);

第二种方式

Intent GPSIntent =new Intent();

GPSIntent.setClassName("com.android.settings",

        "com.android.settings.widget.SettingsAppWidgetProvider");

GPSIntent.addCategory("android.intent.category.ALTERNATIVE");

GPSIntent.setData(Uri.parse("custom:3"));

try {

PendingIntent.getBroadcast(this, 0, GPSIntent, 0).send();

}catch (PendingIntent.CanceledException e) {

e.printStackTrace();

}


关闭gps

Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE

            , android.provider.Settings.Secure.LOCATION_MODE_OFF);

同样也需要添加权限,作为系统apk


获取gps状态

public boolean getGPSState( ) {

LocationManager locationManager= (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

    boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (gps || network) return true;

return false;

}


开关wifi

需要添加权限

android.permission.CHANGE_WIFI_STATE + android.permission.ACCESS_WIFI_STATE

private void switchWiFi(Context context, boolean enabled) {

WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    wm.setWifiEnabled(enabled);

}


限制某个应用的网络权限

需要在源码里面编译,不知道能不能通过反射来实现

PackageManager pm = context.getPackageManager();

List apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);

int uid =0;

for (ApplicationInfo app:apps){

if(app !=null &&app.packageName.equals("packageName")){

uid = app.uid;

    }

}

INetworkManagementService network = INetworkManagementService.Stub.asInterface(

ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));

if ( (uid !=0) && (network !=null) ) {

try{

network.setFirewallUidRule(FIREWALL_CHAIN_STANDBY,uid,NetworkPolicyManager.FIREWALL_RULE_DENY);

//FIREWALL_CHAIN_STANDBY FIREWALL_RULE_DENY   都是2

    }catch(RemoteException e){

e.printStackTrace();

    }

}


开关飞行模式

private void setAirplaneModeOn(boolean enabling,Context context) {

    Settings.Global.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,

            enabling ?1 :0);

    Intent intent =new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);

    intent.putExtra("state", enabling);

    context.sendBroadcast(intent);

}

需要添加android.permission.WRITE_SECURE_SETTINGS权限


时间转换

private long turnTime(String time){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        long millionSeconds =0;

        try {

            millionSeconds = sdf.parse(time).getTime();

        } catch (ParseException e) {

            e.printStackTrace();

        }

        return millionSeconds;

    }

new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(SystemClock.currentThreadTimeMillis());

某一天的时间设置

        Calendar calendar = Calendar.getInstance();

        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        calendar.set(Calendar.MINUTE, 0);

        calendar.set(Calendar.HOUR_OF_DAY, 4);

        calendar.set(Calendar.SECOND, 0);

        calendar.set(Calendar.MILLISECOND, 0);

        long selectTime = calendar.getTimeInMillis();


wifi开关

private void openOrCloseWifiAP(Context context,boolean status){

        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        wifiManager.setWifiApEnabled(wifiManager.getWifiApConfiguration(),status);

    }


你可能感兴趣的:(Android修改记录《一》)