zoukankan      html  css  js  c++  java
  • Android实现系统重新启动

    有些Android版本没有系统重启的功能,非常不方便。需要我们自己开发一个能够重新启动的应用。

    首先定义布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.hzhi.restart"
        android:versionCode="1"
        android:versionName="1.0" 
        android:installLocation="preferExternal"
        android:sharedUserId="android.uid.system">
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.hzhi.restart.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>
    

    布局文件其实很简单,就是一个按钮。注意android:sharedUserId="android.uid.system",这是为了让应用分享一个系统级别的UID,否则会出现权限拒绝的错误。

    类文件:

    package com.hzhi.restart;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    	
    	public void click(View view){
    		
    		Intent intent = new Intent();
    		intent.setAction(Intent.ACTION_REBOOT);
    		intent.putExtra("nowait", 1);
    		intent.putExtra("interval", 1);
    		intent.putExtra("startTime", 1);
    		intent.putExtra("window", 0);
    		sendBroadcast(intent);
    		
    	}
    
    }
    

    运行后会出错,这是因为程序运行时,使用的是系统默认的签名,而不是系统级别的签名。解决方法是将默认的签名删除,替换成系统级别的签名(此过程太繁琐,不再详细叙述)。

  • 相关阅读:
    WebApi接口访问频率控制的实现
    一分钟告诉你究竟DevOps是什么鬼?
    大多数企业不知道的隐形成本
    29个网络营销必须知道的数据
    如何让自己的生活有品质感?
    一则有意思的产品小故事
    免费学习编程的9个地方
    营销,就是营销人性的弱点!
    网络营销行业十大看了就想吐的“滥词”
    高质量的内容是SEO的关键
  • 原文地址:https://www.cnblogs.com/mstk/p/3427258.html
Copyright © 2011-2022 走看看