Android實現(xiàn)系統(tǒng)重新啟動的功能
更新時間:2013年11月19日 10:28:20 作者:
有些Android版本沒有系統(tǒng)重啟的功能,非常不方便。需要我們自己開發(fā)一個能夠重新啟動的應用
首先定義布局文件:
復制代碼 代碼如下:
<?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",這是為了讓應用分享一個系統(tǒng)級別的UID,否則會出現(xiàn)權(quán)限拒絕的錯誤。
類文件:
復制代碼 代碼如下:
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);
}
}
運行后會出錯,這是因為程序運行時,使用的是系統(tǒng)默認的簽名,而不是系統(tǒng)級別的簽名。解決方法是將默認的簽名刪除,替換成系統(tǒng)級別的簽名。
相關(guān)文章
學習使用Material Design控件(四)Android實現(xiàn)標題欄自動縮放、放大效果
這篇文章主要為大家介紹了學習使用Material Design控件的詳細教程,Android實現(xiàn)標題欄自動縮放、放大效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07android自定義view實現(xiàn)數(shù)字進度條
這篇文章主要為大家詳細介紹了android自定義view實現(xiàn)數(shù)字進度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12android中TabHost的圖標(48×48)和文字疊加解決方法
開發(fā)過程中,有時候圖標稍微大點,比如48×48的時候,文字就會和圖標疊加起來,遇到這種問題我們該怎樣處理呢?本文將詳細介紹希望對你有所幫助2013-01-01