在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是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调用远程服务设置系统时间的更多相关文章

  1. QT在linux环境下读取和设置系统时间(通过system来直接调用Linux命令,注意权限问题)

    QT在Linux环境下读取和设置系统时间 本文博客链接:http://blog.csdn.NET/jdh99,作者:jdh,转载请注明. 环境: 主机:Fedora12 开发软件:QT 读取系统时间 ...

  2. Qt设置系统时间(使用SetSystemTime API函数)

    大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime();  ...

  3. C#实现设置系统时间

    using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace Demo { pub ...

  4. Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务

    1. 概述 bindService() 绑定服务  可以得到服务的代理人对象,间接调用服务里面的方法. 绑定服务: 间接调用服务里面的方法.           如果调用者activity被销毁了, ...

  5. date 显示或设置系统时间和日期

    显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...

  6. Linux 设置系统时间和日期 API

    嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码. Linux设置时间和日期的步骤: 1. 设置系统时间和日期: 2. 该系统的时间和日期,同步到硬件. #include ...

  7. ubuntu设置系统时间与网络时间同步

    ubuntu设置系统时间与网络时间同步   Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC).   系统时间:指当前Linux Ker ...

  8. ubuntu设置系统时间与网络时间同步和时区

    Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...

  9. delphi中设置系统时间方法

    procedure TMainFrm.Timer1Timer(Sender: TObject); var   systemtime:Tsystemtime;   dt:TDateTime; begin ...

随机推荐

  1. 初学ReactJS,写了一个RadioButtonList组件

     1 <!DOCTYPE html>  2 <html>  3 <head>  4     <title>React Demo</title> ...

  2. Cinder 组件详解 - 每天5分钟玩转 OpenStack(47)

    本节我们将详细讲解 Cinder 的各个子服务. cinder-api cinder-api 是整个 Cinder 组件的门户,所有 cinder 的请求都首先由 nova-api 处理.cinder ...

  3. 读《linux内核完全注释》的FAQ

    以下只是个人看了<linux内核完全注释>的一点理解,如果有错误,欢迎指正! 1 eip中保存的地址是逻辑地址.线性地址还是物理地址? 这个应该要分情况.eip保存的是下一条要执行的指令地 ...

  4. 坑爹的Maven

    之前没用过Maven,最近在研究Curator的时候,导入别人的工程,但是没有相应的包,需使用Maven解决依赖.于是各种折腾,最后虽然解决了,但中间的坑还不少.尽管网上也有相应的安装教程,但很多都是 ...

  5. sublime3破解版sublime3注册码

    Sublime Text 3 官方网址:http://www.sublimetext.com/ Mac下载地址: http://c758482.r82.cf2.rackcdn.com/Sublime% ...

  6. kafka源码分析之二客户端分析

    客户端由两种:生产者和消费者 1. 生产者 先看一下生产者的构造方法: private KafkaProducer(ProducerConfig config, Serializer<K> ...

  7. [mysql]支持emoji(字符集问题)

    问题的根源 主要问题就是在字符集,一般解决这种问题都是靠试验.我实验了一通,得出的结论和大家分享一下(如有错误,还望指正): 数据库的字符集 数据库连接的字符集 配置方法 设置数据库的字符集为utf8 ...

  8. 微信小程序开发初体验

    微信小程序上线几天了,趁着周末补了一下JS,然后今天参照文档和教程写了个小demo 文档地址       教程地址 看文档就看了一点时间,因为以前没接触过JS框架,但是接触过PHP框架= = ,所以理 ...

  9. 数据库join方式分析

    前言    不管是博客园还是CSDN,看到很多朋友对数据库的理解.认识还是没有突破一个瓶颈 ,而这个瓶颈往往只是一层窗纸,越过了你将看到一个新世界.    04.05年做项目的时候,用SQL Serv ...

  10. 【原创】SQL审核系统

    前言 应公司dba的需求,做了一个sql审核并可以执行.记录的系统,整个系统有三种角色: DBA(需要管理员手动登记) 运维人员(需要管理员手动登记) 开发人员(默认注册后用户) 权限分配如下: 一些 ...