使用AIDL调用远程服务设置系统时间
在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的。于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间的目的。
这里用到的技术有:
1、Signapk签名
2、AIDL
3、bind service
将应用变成系统应用
1、AndroidManifest.xml中加入android:sharedUserId="android.uid.system"
2、使用系统密钥签名。系统签名在Android源码目录中的位置是"build\target\product\security",下面的platform.pk8和platform.x509.pem两个文件。然后用Android提供的Signapk工具来签名,signapk的源代码是在"build\tools\signapk"下,用法为"signapk platform.x509.pem platform.pk8 input.apk output.apk"
时间设置服务 CustomServices
ICustomServices.aidl 里定义了设置日期和设置时间方法
interface ICustomServices { void setDate(int year,int month,int day);
void setTime(int hourOfDay, int minute); }
CustomService.Java
public class CustomService extends Service {
private static final String TAG = CustomService.class.getSimpleName(); private MyBinder mBinder; @Override
public void onCreate() {
super.onCreate(); if (mBinder == null) {
mBinder = new MyBinder();
} } @Override
public IBinder onBind(Intent intent) {
return mBinder;
} class MyBinder extends ICustomServices.Stub { @Override
public void setDate(int year, int month, int day) throws RemoteException { setDate(CustomService.this, year, month - 1, day);
} @Override
public void setTime(int hourOfDay, int minute) throws RemoteException {
setTime(CustomService.this, hourOfDay, minute);
} void setDate(Context context, int year, int month, int day) {
Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
long when = c.getTimeInMillis(); if (when / 1000 < Integer.MAX_VALUE) {
((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
}
} void setTime(Context context, int hourOfDay, int minute) {
Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long when = c.getTimeInMillis(); if (when / 1000 < Integer.MAX_VALUE) {
((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
}
}
} }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rs.customservices" android:sharedUserId="android.uid.system">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"> <service
android:name="com.rs.customservices.CustomService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.rs.CustomService" />
</intent-filter>
</service>
</application> </manifest>
编译完后使用系统签名将APK文件签名成系统应用。
客户程序
将上面工程中的 ICustomServices.aidl 拷入到客户工程中,注意:包的目录结构也需要拷入。
CustomServiceActivity.java
public class CustomServiceActivity extends Activity {
private static final String TAG="CustomServiceActivity"; ICustomServices mCustomServices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_service); Intent intentCust = new Intent();
intentCust.setAction("com.rs.CustomService");
//在5.0及以上版本必须要加上这个
intentCust.setPackage("com.rs.customservices");
bindService(intentCust, mServiceConnection, Context.BIND_AUTO_CREATE);
} ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mCustomServices = ICustomServices.Stub.asInterface(iBinder); Log.i(TAG,"mServiceConnection2 onServiceConnected"); try {
mCustomServices.setDate(1999, 5,6);
mCustomServices.setTime(5,45);
} catch (RemoteException e) {
e.printStackTrace();
} } @Override
public void onServiceDisconnected(ComponentName componentName) { }
}; @Override
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConnection);
}
}
使用AIDL调用远程服务设置系统时间的更多相关文章
- QT在linux环境下读取和设置系统时间(通过system来直接调用Linux命令,注意权限问题)
QT在Linux环境下读取和设置系统时间 本文博客链接:http://blog.csdn.NET/jdh99,作者:jdh,转载请注明. 环境: 主机:Fedora12 开发软件:QT 读取系统时间 ...
- Qt设置系统时间(使用SetSystemTime API函数)
大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime(); ...
- C#实现设置系统时间
using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace Demo { pub ...
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
1. 概述 bindService() 绑定服务 可以得到服务的代理人对象,间接调用服务里面的方法. 绑定服务: 间接调用服务里面的方法. 如果调用者activity被销毁了, ...
- date 显示或设置系统时间和日期
显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...
- Linux 设置系统时间和日期 API
嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码. Linux设置时间和日期的步骤: 1. 设置系统时间和日期: 2. 该系统的时间和日期,同步到硬件. #include ...
- ubuntu设置系统时间与网络时间同步
ubuntu设置系统时间与网络时间同步 Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Ker ...
- ubuntu设置系统时间与网络时间同步和时区
Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...
- delphi中设置系统时间方法
procedure TMainFrm.Timer1Timer(Sender: TObject); var systemtime:Tsystemtime; dt:TDateTime; begin ...
随机推荐
- Android全屏(包含3种隐藏顶部状态栏及标题栏和一种隐藏Android 4.0平板底部状态栏的方法)
http://www.xuebuyuan.com/558284.html 方法一 public class MainActivity extends Activity { @Override prot ...
- java中两种添加监听器的策略
/*第一种:将事件的处理委托给其他对象,下面的例子是委托给了MyListener(implements ActionListener)*/ 1 import java.applet.Applet; i ...
- iOS开发之使用CocoaPods更新第三方出现“target overrides the `OTHER_LDFLAGS`……”问题解决方案
今天在自己的项目中用CocoaPods引入第三方SDWebImage的时候,出现了问题.当更新完毕后,在终端没太注意这个问题的提示,就直接使用SDWebImage了,在使用的时候一些方法的提示和头文件 ...
- 辗转相除法求最大公约数,非goto
#include<iostream> using namespace std; //不推荐用goto,当然用它更快 //辗转相除法求两数的最大公约数 int gcd(long int a, ...
- 配置React Native环境
一. 安装Homebrew: “Homebrew installs the stuff you need that Apple didn’t.——Homebrew OS X 更完整”. Homebr ...
- 如何在ios中集成微信登录功能
在ios中集成微信的登录功能有两种方法 1 用微信原生的api来做,这样做的好处就是轻量级,程序负重小,在Build Settings 中这样设置 然后设置 友盟的设置同上,但是要注意,加入你需要的所 ...
- [ES] 基础概念
Elasticsearch是基于Luence实现的一款搜索引擎,支持分布式和集群,并且搜索近实时,主要用于搜索和数据分析 索引 index 可以理解为数据库中的database,存储的是实际数据,因为 ...
- 1Z0-053 争议题目解析501
1Z0-053 争议题目解析501 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 501.Note the output of the following query; SQL&g ...
- 【集合框架】JDK1.8源码分析之Collections && Arrays(十)
一.前言 整个集合框架的常用类我们已经分析完成了,但是还有两个工具类我们还没有进行分析.可以说,这两个工具类对于我们操作集合时相当有用,下面进行分析. 二.Collections源码分析 2.1 类的 ...
- 转:C语言中的头文件可以自己写吗?
转自:http://www.eefocus.com/computer00/blog/08-09/155791_9ebdc.html 一些初学C语言的人,不知道头文件(*.h文件)原来还可以自己写的. ...