使用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调用Jni,非常简单的一个Demo
step1:创建一个android项目 Project Name:jnitest Build Target: Android 1.6 Application Nam ...
- MySQL学习笔记十三:表分区
1.分区一般用于非常大的表,采用“分而治之”的策略,将一个很大的对象分成多个小对象进行管理,每个分区都是一个独立的对象. 分区使用分区键将数据根据范围值,特定列值或HASH值等规则分布在不同的分区中. ...
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...
- 【解决】查询无法完成,因为其包含的查找列数已超过管理员强制实施的查找列阈值。Error code=0x80070093; Error source=Groove
前因: 修改了SharePoint Server 2013 下面的文档库的名称,原先2个汉字,现在8个汉字.结果,SkyDrive Pro 就无法同步了,无论是停止重新同步还是手动填写进行同步都不可以 ...
- 用backbone实现的一个MVC的小demo
一.Apache配置 本实例需要使用php支持.要现在Apache中配置虚拟目录,在Apache下的httpd-vhosts.conf文件中添加如下代码 <VirtualHost *:80> ...
- Oracle Tuning 基础概述01 - Oracle 常见等待事件
对Oracle数据库整体性能的优化,首先要关注的是在有性能问题时数据库排名前几位等待事件是哪些.Oracle等待事件众多,随着版本的升级,数量还在不断增加,可以通过v$event_name查到当前数据 ...
- Android之ListView的getItemViewType和getViewTypeCount
PS:感觉这两个方法其实还是很容易理解的,也算是给我其他两个朋友写的吧,帮他们搞清楚这两个方法的用法和概念.同时还有一些小细节问题需要注意. 学习内容: 1.getItemViewType和getVi ...
- 浅析String不可变性
在所有编程语言领域,我想字符串应该是地球上最常用的表达手段了吧. 在java的世界里,String是作为类出现的,核心的一个域就是一个char数组,内部就是通过维护一个不可变的char数组,来向外部输 ...
- let与const的区别
let与const都是只在声明所在的块级作用域内有效. let声明的变量可以改变,值和类型都可以改变,没有限制. const声明的变量不得改变值,这意味着,const一旦声明变量,就必须立即初始化,不 ...
- STM32Cube Uart_DMA测试工程
1.打开软件,新建工程,选择芯片信号,这里选择 2.USART1使能选择"Asynchronous"模式: 3.配置"RCC",High ...