此文章仅作为学习交流所用
转载或引用请务必注明原文地址:http://blog.csdn.net/ningchao328/article/details/51513898
谢谢!
--------------------------------------------------------------------------------------------------------------------
客户需求:编写一个测试的app,能过控制系统的关机和重启
解决思路:通过简单的两个按钮,通过发送广播来实现关机和重启(这里是进程间的通信啦就用广播来做了),其实这个两个广播在我们的系统是已经注册好了的,我们做的就是发送符合系统注册的广播即可。这里要注意的就是要让我们测试的app具有系统权限,相当于是变成系统认可的系统app啦。
应用程序界面:
1.eclipse里面新建一个 android 工程 RebootApp
2. 编写 Android.mk, 下面是此文件内的相应内容:
#
# Copyright (C) 2008 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := reboot
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
编写完后将此文件添加到工程RebootApp里,位置如图:
在系统目录下位置如图:
3. 工程 RebootApp里补充编写AndroidMenifest.xml
补充内容: android:sharedUserId="android.uid.system"
内容位置:
4. 工程 RebootApp里编写逻辑代码 MainActivity.java (布局文件就省略,就两个按钮的布局)
package com.zhc.rbootapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
//打开
public void sendOpen(View v){
//Log.v(TAG, "broadcast->shutdown");
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
//其中false换成true,会弹出是否关机的确认窗口
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
//关闭
public void sendClose(View v){
Intent intent2 = new Intent(Intent.ACTION_REBOOT);
intent2.putExtra("nowait", 1);
intent2.putExtra("interval", 1);
intent2.putExtra("window", 0);
sendBroadcast(intent2);
}}
注意:这两个在eclipse里面会报错(所以不要用eclipse来进行编译,用xshell5来编译):
Intent.ACTION_REQUEST_SHUTDOWN
Intent.EXTRA_KEY_CONFIRM
不要担心放到系统编译就可以了,注意下面的步骤即可。
1. 将整个RbootApp工程目录放到 android 源码里面编译.
路径:Y:\r58_android4.4_v1.2\android\packages\apps(Y:\r58_android4.4_v1.2\android这个就因人而异了,结合自己的实际情况即可。一定得在这个目录下xxx\packages\apps我就没去尝试了)。
2. 然后再用编译工具xshell5(在这个工具里面编译的详细命令就不写了)进入到Y:\r58_android4.4_v1.2\android\packages\apps\RbootApp编译即可得到apk
1. 然后在这个路径下:
Y:\r58_android4.4_v1.2\android\out\target\product\octopus-zhizhiwu\system\app(这目录你也得结合自己的实际情况了)找到reboot.apk
用adb命令adb install reboot.apk即可。
在此过程中遇到的问题:
问题1:布局问题,看来布局还是得附上了
错误解决参照博客:http://www.oschina.net/question/163910_27978?fromerr=jI4672Gi
问题2:Permissionis only granted to system apps(权限仅授予系统应用)这个问题是在导入到eclipse里遇到的,不到入eclipse就不会遇到,导入eclipse是个人习惯eclipse查看文件位置。
解决参照博客:http://blog.csdn.net/gaojinshan/article/details/14230673
本文详细参照 博客:
1. http://www.jb51.net/article/79940.htm
2.http://blog.csdn.net/luzhenrong45/article/details/42092007
3.http://www.2cto.com/kf/201412/363630.html
4. http://blog.csdn.net/gaojinshan/article/details/14230673
5. http://www.oschina.net/question/163910_27978?fromerr=jI4672Gi