使用AIDL调用远程服务设置系统时间

时间:2021-12-16 05:09:51

在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的。于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间的目的。

使用AIDL调用远程服务设置系统时间

这里用到的技术有:

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

使用AIDL调用远程服务设置系统时间

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 拷入到客户工程中,注意:包的目录结构也需要拷入。

使用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);
}
}