Android开发之调用系统隐藏API

news/2024/7/4 8:29:37

Android中有些Api被隐藏了,比如开关机,重启,如果要在应用中调用,要么用反射,要么就是直接在Android源代码下编译。如果用Eclipse用IDE,尽管调用隐藏API的代码能顺利在Android源码下通过编译,但Eclipse找不到相应的类,会报错,为了写代码时方便点,我们可以把相关的包加到eclipse中。
先下载Android源码(本站有提供4.2.2的打包下载链接,自己找找),编译一次,找到out/target/common/obj/JAVA_LIBRARIES/fmradioif_intermediates/classes.jar,在Eclispe中加到我们自己项目的构建路径中,这样就可以调用到相关的类了。另外,这个包里的有些类和方法,跟android.jar是有冲突的,我们得手工调整优先级,打开项目属性,找到Java构建路径,排序和导出,把刚刚添加的classes.jar放到最顶上。
另外,我们需要调用的API,源代码都在frameworks/base/core/java下,根据包名找。
下面的例子是关机的,项目名为PowerOff。

package com.example.poweroff;
 
import android.app.Activity;
import android.os.Bundle;
import android.os.IPowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
IPowerManager                     power=IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
 
if(power!=null)
{packages/apps
try {
/**
* 查看frameworks/base/core/java/android/os/IPowerManager.aidl
* void shutdown(boolean confirm, boolean wait);
* 所以,第一个是是否让用户确认,第二个是是否等待
*/
power.shutdown(false,false);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
 
@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;
}
 
}

AndroidManifest.xml:需要重启权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.poweroff"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <!-- 加入这条,Eclipse会报错,提示Permission is only granted to system apps,只有系统app才能调用,没关系,反正我们不用eclipse编译 -->
   <uses-permission android:name="android.permission.REBOOT" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.poweroff.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.mk文件,内容如下:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := PowerOff
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)

注意LOCAL_MODULE_TAGS := optional,可能有四个值user,eng,tests,optional,对应你编译Android源码时的参数,optional兼容所有。
另外,布局的xml中,android:text属性要用引用,先写到strings.xml里,直接hard code会报错.
编译:
切到android源代码根目录,设环境变量 执行/build/envsetup.sh
把项目拷到packages/apps目录下,切换到packages/apps/PowerOff目录,把Eclipse自动生成的gen目录删除,执行mm开始编译。
成功后,会提示Install: out/target/product/maguro/system/app/PowerOff.apk
但是正常能安装的文件在 out/target/product/maguro/obj/APPS/PowerOff_intermediates/package.apk,如果安装上面那个,可能会出现INSTALL_FAILED_DEXOPT错误
再用 adb install装上,成功关机.
有一点需要注意,我的手机是Galaxy Nexus,刚刚刷上自己用源码编译的Rom,如果是其他手机,还是需要系统签名先。

转自:http://m.blog.csdn.net/blog/androidstudio_11109/18989535



http://www.niftyadmin.cn/n/3649247.html

相关文章

Delphi之东进数字语音卡(SS1)可复用源码

Delphi之东进数字语音卡(SS1)可复用源码作者&#xff1a;成晓旭Blog&#xff1a;http://blog.csdn.net/cxxsoft(声明&#xff1a;欢迎转载&#xff0c;请保证文章的完整性)由于工作原因&#xff0c;本人将不在从事商业应有软件的开发工作&#xff0c;现在开始逐级“解密”自己以…

沉浸式状态栏——状态栏的实现封装类

沉浸式状态栏有两种方式&#xff1a;一&#xff1a;当状态栏下面是一张图片的时候&#xff0c;设置上面状态栏为透明&#xff0c;这样状态栏就会被图片覆盖&#xff08;这里为方便我设置了一个button颜色为#FF4081&#xff09; 首先看下没有设置沉浸式状态栏时的状态栏: 设置状…

得到了国内第一本《And I thought I Knew QTP》

承蒙作者厚爱&#xff0c;得到了一本签名版的书&#xff0c;撒几张照片&#xff1a; 加我微博吧&#xff0c;分享更多测试讯息&#xff1a;http://www.weibo.com/quicktest 作者在米国发行此书的照片&#xff1a;

Android之探秘蓝牙隐藏API

上次讲解Android的蓝牙基本用法&#xff0c;这次讲得深入些&#xff0c;探讨下蓝牙方面的隐藏API。用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对&#xff0c;但是这两项功能的函数没有在SDK中给出&#xff0c;那么如何去使用这两项功能呢&#x…

golang flag 包_如何在Go中使用Flag包

golang flag 包介绍 (Introduction) Command-line utilities are rarely useful out of the box without additional configuration. Good defaults are important, but useful utilities need to accept configuration from users. On most platforms, command-line utilities…

Ubuntu 装 Samba 安装详解 (已在Ubuntu11Server测试通过)

转自&#xff1a;http://wiki.ubuntu.org.cn/Samba Samba Samba是Ubuntu和Windows进行网络共享的工具&#xff0c;比如分享打印机&#xff0c;互相之间传输资料文件。 目录 [隐藏]1 安装Samba2 Kubuntu3 配置4 Ubuntu访问windows xp/2003/2000系统5 排错 5.1 乱码 6 smb用户…

golang接口的使用场景_如何在Go中使用接口

golang接口的使用场景介绍 (Introduction) Writing flexible, reusable, and modular code is vital for developing versatile programs. Working in this way ensures code is easier to maintain by avoiding the need to make the same change in multiple places. How you…