android源代码中一个很疑惑的问题

时间:2021-10-30 16:18:15
android 源代码中打开GPS的方法很简单,就一个方法:

   /**
     * Toggles the state of GPS.
     *
     * @param context
     */
    private void toggleGps(Context context)
    {
        ContentResolver resolver = context.getContentResolver();
        boolean enabled = getGpsState(context);

       // 这个方法在SDK中不存在
        Settings.Secure.setLocationProviderEnabled(resolver,
                LocationManager.GPS_PROVIDER, !enabled);
    }

用现有的SDK接口是不能打开和关闭GPS的,那是不是自己去编译android源代码,就可以提供这个方法并可以调用?

我觉得这样是可以解决编译的问题,但是在实际运行时能否成功调用?谢谢!

13 个解决方案

#1


应该可以。。。。。。。。。。。。。。

#2


可以的,没什么问题,本来SDK中的API就不全

#3


关注,试试先

#4


SDK中有很大一部分元素用@HIDE隐藏掉了,可恶

#5


这种问题是存在的
我的办法是在classpath里 用一个同名的自己编的class替换并输出

#6


你可以到source code里面把@hide标签下掉,然后重新编译.就OK了.

或者你可以给我邮箱, 我发包framework.jar给你。

#7


支持问题,顶

#8


study........

#9


就这个问题我深究了下去

到目前为止没有完美的解决方法,首先解决编译问题很简单,随便写个类写个方法能编译通过就行

但是编译完了还不够,这些不开放的接口需要system权限,加了权限还不行,还需要system级的签名,要想在自制系统上运行,用sdk签名就可以了,但是各个手机厂商都有自己的密钥

所以最终这种解决方法只能在自制系统上用,想弄个通用的版本。还需要SDK提供的接口

#10


你把android source code中你需要的函数的@hide标签下掉,然后编译出来一个jar,导入到你的工程中就OK了。

我平常是这么做的,程序在N one和A60上是可以正常运行的。

#11


我是通过这个来打开gps的。

               Intent intent=new Intent("android.location.gps.setenable");
                intent.putExtra("enabled", true);
                mContext.sendBroadcast(intent);

#12


引用 11 楼 yyy025025025 的回复:
我是通过这个来打开gps的。

               Intent intent=new Intent("android.location.gps.setenable");
                intent.putExtra("enabled", true);
                mContext.sendBroadcast(intent);


这个貌似在2.01不能用了

#13


要有permission,同时你的app要安装system/app下。       
 ContentResolver resolver = context.getContentResolver();
 Settings.Secure.setLocationProviderEnabled(resolver, LocationManager.GPS_PROVIDER,
                true);

这个是活的gps状态的代码
        ContentResolver resolver = context.getContentResolver();
        return Settings.Secure.isLocationProviderEnabled(resolver, LocationManager.GPS_PROVIDER);

#1


应该可以。。。。。。。。。。。。。。

#2


可以的,没什么问题,本来SDK中的API就不全

#3


关注,试试先

#4


SDK中有很大一部分元素用@HIDE隐藏掉了,可恶

#5


这种问题是存在的
我的办法是在classpath里 用一个同名的自己编的class替换并输出

#6


你可以到source code里面把@hide标签下掉,然后重新编译.就OK了.

或者你可以给我邮箱, 我发包framework.jar给你。

#7


支持问题,顶

#8


study........

#9


就这个问题我深究了下去

到目前为止没有完美的解决方法,首先解决编译问题很简单,随便写个类写个方法能编译通过就行

但是编译完了还不够,这些不开放的接口需要system权限,加了权限还不行,还需要system级的签名,要想在自制系统上运行,用sdk签名就可以了,但是各个手机厂商都有自己的密钥

所以最终这种解决方法只能在自制系统上用,想弄个通用的版本。还需要SDK提供的接口

#10


你把android source code中你需要的函数的@hide标签下掉,然后编译出来一个jar,导入到你的工程中就OK了。

我平常是这么做的,程序在N one和A60上是可以正常运行的。

#11


我是通过这个来打开gps的。

               Intent intent=new Intent("android.location.gps.setenable");
                intent.putExtra("enabled", true);
                mContext.sendBroadcast(intent);

#12


引用 11 楼 yyy025025025 的回复:
我是通过这个来打开gps的。

               Intent intent=new Intent("android.location.gps.setenable");
                intent.putExtra("enabled", true);
                mContext.sendBroadcast(intent);


这个貌似在2.01不能用了

#13


要有permission,同时你的app要安装system/app下。       
 ContentResolver resolver = context.getContentResolver();
 Settings.Secure.setLocationProviderEnabled(resolver, LocationManager.GPS_PROVIDER,
                true);

这个是活的gps状态的代码
        ContentResolver resolver = context.getContentResolver();
        return Settings.Secure.isLocationProviderEnabled(resolver, LocationManager.GPS_PROVIDER);