欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android实现系统重新启动的功能

时间:2021-03-30 09:07:46|栏目: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);

    }

}

运行后会出错,这是因为程序运行时,使用的是系统默认的签名,而不是系统级别的签名。解决方法是将默认的签名删除,替换成系统级别的签名。

上一篇:Android仿新版微信浮窗效果

栏    目:Android代码

下一篇:Android RecyclerView实现数据列表展示效果

本文标题:Android实现系统重新启动的功能

本文地址:http://www.codeinn.net/misctech/91022.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有