android应用实现重启系统

来源:互联网 发布:虚拟主机织梦cms安装 编辑:程序博客网 时间:2024/06/05 16:28

1.在AndroidManifest.xml文件的manifest标签中加入一条Android:sharedUserId="android.uid.system"

[html] view plain copy
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.ipanel.update"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0"   
  5.     android:sharedUserId="android.uid.system" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="9"  
  9.         android:targetSdkVersion="15" />     
  10.     <uses-permission android:name="android.permission.INTERNET"/>  
  11.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  12.     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />  
  13.   
  14.     <application  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name=".MainActivity"  
  20.             android:label="@string/title_activity_main" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  


2.在MainActivity中,有以下2种方式实现:

[java] view plain copy
  1. /*Intent reboot = new Intent(Intent.ACTION_REBOOT);   
  2.     reboot.putExtra("nowait", 1);   
  3.     reboot.putExtra("interval", 1);   
  4.     reboot.putExtra("window", 0);   
  5.     sendBroadcast(reboot); */   
  6. PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);  
  7. pManager.reboot("");  


整个代码:

[java] view plain copy
  1. package com.demo.reboot;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. import android.app.Activity;  
  8. import android.app.AlertDialog;  
  9. import android.content.Context;  
  10. import android.content.DialogInterface;  
  11. import android.content.Intent;  
  12. import android.os.Bundle;  
  13. import android.os.PowerManager;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.           
  25.         Button rebootBtn = (Button) findViewById(R.id.button2);  
  26.         rebootBtn.setOnClickListener(new OnClickListener() {  
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 new AlertDialog.Builder(MainActivity.this)  
  30.                 .setTitle("提示")  
  31.                 .setMessage("确认重启么?")  
  32.                 .setPositiveButton("重启"new DialogInterface.OnClickListener() {  
  33.                     @Override  
  34.                     public void onClick(DialogInterface dialog, int which) {  
  35.                         // 重启  
  36.                         /*String str = "重启"; 
  37.                         try { 
  38.                             str = runCmd("reboot", "/system/bin"); 
  39.                         } catch (IOException e) { 
  40.                             e.printStackTrace(); 
  41.                         }*/  
  42.                         /*Intent reboot = new Intent(Intent.ACTION_REBOOT);   
  43.                         reboot.putExtra("nowait", 1);   
  44.                         reboot.putExtra("interval", 1);   
  45.                         reboot.putExtra("window", 0);   
  46.                         sendBroadcast(reboot); */   
  47.                         PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);  
  48.                         pManager.reboot("重启");  
  49.                         System.out.println("execute cmd--> reboot\n" + "重启");  
  50.                     }  
  51.                 })  
  52.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  53.                     @Override  
  54.                     public void onClick(DialogInterface dialog, int which) {  
  55.                         // 取消当前对话框  
  56.                         dialog.cancel();  
  57.                     }  
  58.                 }).show();  
  59.             }  
  60.               
  61.         });  
  62.     }  
  63.   
  64. }  

 

3.给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 为签名之后应用)

0 0