android系统的重启apk
2016-11-23 20:38
204 查看
android系统的重启apk并不是用普通的应用级别的apk文件来实现的,而是用系统界别的apk来实现重启功能的!
java代码
package com.example.rebooot;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button rebootBtn = (Button) findViewById(R.id.button2);
rebootBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("import")
.setMessage("are you sure reboot")
.setPositiveButton("reboot", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//reboot
/*String str = "reboot";
try {
str = runCmd("reboot", "/system/bin");
} catch (IOException e) {
e.printStackTrace();
}*/
Intent reboot = new Intent(Intent.ACTION_REBOOT);
reboot.putExtra("nowait", 1);
reboot.putExtra("interval", 1);
reboot.putExtra("window", 0);
sendBroadcast(reboot);
PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);
pManager.reboot("reboot");
System.out.println("execute cmd--> reboot\n" + "reboot");
}
})
.setNegativeButton("not", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
});
}
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rebooot"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.system" >
<uses-sdk
android:minSdkVersion="8"
4000
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.SHUTDOWN"/>
<uses-permission android:name="android.permission.REBOOT"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.example.rebooot.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
需要注意的是这里必须获取系统权限 manifest 下面要写android:sharedUserId="android.uid.system"
然后在项目的根目录下创建Android.mk
代码如下
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := rebooot
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
这是项目的全部代码了 下一步就是要编译了
在ubuntu linux 下 把刚才的项目放在源码的packages/apps下面
然后用mm来编译
java代码
package com.example.rebooot;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button rebootBtn = (Button) findViewById(R.id.button2);
rebootBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("import")
.setMessage("are you sure reboot")
.setPositiveButton("reboot", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//reboot
/*String str = "reboot";
try {
str = runCmd("reboot", "/system/bin");
} catch (IOException e) {
e.printStackTrace();
}*/
Intent reboot = new Intent(Intent.ACTION_REBOOT);
reboot.putExtra("nowait", 1);
reboot.putExtra("interval", 1);
reboot.putExtra("window", 0);
sendBroadcast(reboot);
PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);
pManager.reboot("reboot");
System.out.println("execute cmd--> reboot\n" + "reboot");
}
})
.setNegativeButton("not", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
});
}
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rebooot"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.system" >
<uses-sdk
android:minSdkVersion="8"
4000
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.SHUTDOWN"/>
<uses-permission android:name="android.permission.REBOOT"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.example.rebooot.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
需要注意的是这里必须获取系统权限 manifest 下面要写android:sharedUserId="android.uid.system"
然后在项目的根目录下创建Android.mk
代码如下
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := rebooot
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
这是项目的全部代码了 下一步就是要编译了
在ubuntu linux 下 把刚才的项目放在源码的packages/apps下面
然后用mm来编译
相关文章推荐
- Android系统剪裁后卸载APK失败重启系统
- 在APK中重启Android系统
- android APK实现系统重启
- Android 应用程序(APK) 如何获得系统签名权限 强制关闭程序(后台进程)
- Android 系统是否要重启时弹出框
- 利用源码编译Android系统APK
- 让Android程序获得系统的权限,实现关机重启,静默安装等功能
- Android Studio自动生成带系统签名的apk
- Android平台上如何让应用程序获得系统权限以及如何使用platform密钥给apk签名
- android app被系统回收后重启异常的解决方法
- 如何删除android系统只读权限的APK
- 代码实现Android系统重启
- Android 使用系统限制的权限的apk应安装到 /system/app 目录下
- android 应用重启系统 需要系统签名
- Android 系统重启与关机:Java 代码实现
- apk获得Android系统权限的方法
- 将apk打包成系统级应用并且使用静默安装重启机器
- Android系统应用手动安装升级版重启还原问题
- Android root环境下的一些可用操作(关机命令,系统时间,重启命令)
- Android实现系统关机和重启