Android 应用实现系统重启reboot

在AndroidManifest.xml加入权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alex.reboot"
    android:versionCode="1"
    android:versionName="1.0"
    android:sharedUserId="android.uid.system" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="25" />
 <uses-permission android:name="android.permission.INTERNET"/>  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />  
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <activity
            android:name=".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>

实现系统重启关键代码

package com.alex.reboot;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
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
    protected 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("提示")  
                .setMessage("确认重启么?")  
                .setPositiveButton("重启", new DialogInterface.OnClickListener() {  
                    @Override  
                    public void onClick(DialogInterface dialog, int which) {  
                        // 重启  
                        PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);  
                        pManager.reboot("重启");  
                    }  
                })  
                .setNegativeButton("取消", 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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

给apk签名

签名方法:
1)添加权限
在AndroidManifest.xml文件下添加android:sharedUserId=”android.uid.system” 。

2)在Eclipse中导出无签名的应用文件
在工程中:右键->Android Tools -> Export Unsigned Application Package导出应用

3)找出系统签名密钥
系统密钥为:platform.pk8和platform.x509.pem
路径: build\target\product\security

4)找出系统签名工具
工具为:signApk.jar
路径:/out/host/Linux-x86/framework/ signApk.jar

5)开始签名
将第2、3、4步找到的无签名应用、platform.pk8、platform.x509.pem和signApk.jar放到同一文件夹下如F:\sign。
打开 dos 操作界面,定们到F:\sign,输入命令:
Java -jar signapk.jar platform.x509.pem platform.pk8 a.apk b.apk
(a.apk 为未签名应用 b.apk 为签名之后应用)
运行效果:
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值