您的位置:首页 > 移动开发 > Android开发

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来编译
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: