在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是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. .Net 转战 Android 4.4 日常笔记(10)--ADT集成环境更新SDK

    今天下载了一份原来来参考,却发现SDK版本偏低我没有安装 用SDK Manager却一直更新不了出现 Failed to fetch URL https://dl-ssl.google.com/and ...

  2. MongoDB学习系列(2)--使用PHP访问MongoDB

    第一部分:介绍 在Windows上安装最新MongoDB步骤非常的简单,这里不做介绍.但是如果你安装的时候没有将MongoDB作为服务运行,每次你都要使用cmd切换到指定的目录下,然后在cmd中启动M ...

  3. BFC之清除浮动篇&clear

    我们在日常代码生活中,或多或少会利用浮动来布局,例如导航布局,如下图所示: 但是,我们在实现的时候,经常会遇到父元素“塌陷”以及清除浮动问题.例如 <!DOCTYPE html> < ...

  4. 最大值最小化(DP)

    题目来源:网易有道2013年校园招聘面试一面试题 题目描述: 在印刷术发明之前,复制一本书是一个很困难的工作,工作量很大,而且需要大家的积极配合来抄写一本书,团队合作能力很重要.当时都是通过招募抄写员 ...

  5. ASP.NET MVC 混搭 ASP.NET WebForms 所导致的 Html.ActionLink/BeginForm 问题

    首先,需要了解下这篇博文:<ASP.NET WebForms MapPageRoute 路由配置> 之前,在 ASP.NET MVC 中混搭 ASP.NET WebForms,使用 Map ...

  6. How to implement a neural network

    神经网络的实践笔记 link: http://peterroelants.github.io/posts/neural_network_implementation_part01/ 1. 生成训练数据 ...

  7. [OpenCV] Convolutional Neural Network

    Ref: 从LeNet-5看卷积神经网络CNNs 关于这篇论文的一些博文的QAC: 1. 基本原理 MLP(Multilayer Perceptron,多层感知器)是一种前向神经网络(如下图所示),相 ...

  8. C语言 第三章 基础编程测试与练习

    1.屏幕上输出:This is a C program 2.输入两个整数,求两个数的和,如下所示:请输入第1个数:5请输入第2个数:3 3加5的和是8 3.完成华氏温度与摄氏温度间的转换,如下所示:请 ...

  9. Android APK如何签名

    Android项目以它的包名作为唯一标识,如果在同一设备上安装两个相同的应用,后面安装的应用就会覆盖前面安装的应用.为了避免这种情况的发生,我们需要对作为产品发布的应用进行签名. 签名其实有两个作用: ...

  10. ZOJ Problem Set - 1045 HangOver

    #include <stdio.h> int main() { float c; int i; while(scanf("%f",&c)!=EOF&&a ...