Use AIDL to call the remote service and set the system time.

Source: Internet
Author: User

Use AIDL to call the remote service and set the system time.

 

In actual work, users often need to set the system time using code, but Android non-system applications cannot set the system time. Therefore, I designed a time setting service that uses the system signature. The customer can set the time by calling the methods in the service through the bind.

 

The technologies used here are:

1. Signapk Signature

2. AIDL

3. bind service

 

Turn an application into a system application

1. Add android: sharedUserId = "android. uid. system" to AndroidManifest. xml"

2. Use the system key signature. The location of the system signature in the Android source code directory is "build \ target \ product \ security". The following two files are platform. pk8 and platform. x509.pem. Then use the Signapk tool provided by Android to sign it. The source code of signapk is in "build \ tools \ signapk" and its usage is "signapk platform. x509.pem platform. pk8 input.apk output.apk"

 

Time Setting service CustomServices

 

ICustomServices. aidl defines the date setting and the time setting method.

1 interface ICustomServices {2 3      void setDate(int year,int month,int day);4      void setTime(int hourOfDay, int minute);5 6 }

CustomService.Java  
 1 public class CustomService extends Service { 2     private static final String TAG = CustomService.class.getSimpleName(); 3  4     private MyBinder mBinder; 5  6     @Override 7     public void onCreate() { 8         super.onCreate(); 9 10         if (mBinder == null) {11             mBinder = new MyBinder();12         }13 14     }15 16 17     @Override18     public IBinder onBind(Intent intent) {19         return mBinder;20     }21 22 23     class MyBinder extends ICustomServices.Stub {24 25         @Override26         public void setDate(int year, int month, int day) throws RemoteException {27 28             setDate(CustomService.this, year, month - 1, day);29         }30 31         @Override32         public void setTime(int hourOfDay, int minute) throws RemoteException {33             setTime(CustomService.this, hourOfDay, minute);34         }35 36         void setDate(Context context, int year, int month, int day) {37             Calendar c = Calendar.getInstance();38 39             c.set(Calendar.YEAR, year);40             c.set(Calendar.MONTH, month);41             c.set(Calendar.DAY_OF_MONTH, day);42             long when = c.getTimeInMillis();43 44             if (when / 1000 < Integer.MAX_VALUE) {45                 ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);46             }47         }48 49         void setTime(Context context, int hourOfDay, int minute) {50             Calendar c = Calendar.getInstance();51 52             c.set(Calendar.HOUR_OF_DAY, hourOfDay);53             c.set(Calendar.MINUTE, minute);54             c.set(Calendar.SECOND, 0);55             c.set(Calendar.MILLISECOND, 0);56             long when = c.getTimeInMillis();57 58             if (when / 1000 < Integer.MAX_VALUE) {59                 ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);60             }61         }62     }63     64 }

 

AndroidManifest. xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3     package="com.rs.customservices" android:sharedUserId="android.uid.system"> 4     <application 5         android:allowBackup="true" 6         android:icon="@mipmap/ic_launcher" 7         android:label="@string/app_name" 8         android:supportsRtl="true" 9         android:theme="@style/AppTheme">10 11 12         <service13             android:name="com.rs.customservices.CustomService"14             android:enabled="true"15             android:exported="true">16             <intent-filter>17                 <action android:name="com.rs.CustomService" />18             </intent-filter>19         </service>20     </application>21 22 </manifest>

 

 

After compilation, use the system signature to sign the APK file into the system application.

 

 

Customer Program

Import ICustomServices. aidl packages from the above project to the customer project. Note: the directory structure of the package also needs to be merged.

 

CustomServiceActivity.java

1 public class CustomServiceActivity extends Activity {2 private static final String TAG = "CustomServiceActivity"; 3 4 ICustomServices mCustomServices; 5 @ Override 6 protected void onCreate (Bundle savedInstanceState) {7 super. onCreate (savedInstanceState); 8 setContentView (R. layout. activity_custom_service); 9 10 Intent intentCust = new Intent (); 11 intentCust. setAction ("com. rs. customService "); 12 // This 13 intentCust must be added in version 5.0 or later. setPackage ("com. rs. customservices "); 14 bindService (intentCust, mServiceConnection, Context. BIND_AUTO_CREATE); 15} 16 17 ServiceConnection mServiceConnection = new ServiceConnection () {18 @ Override19 public void onServiceConnected (ComponentName componentName, IBinder iBinder) {20 mCustomServices = ICustomServices. stub. asInterface (iBinder); 21 22 Log. I (TAG, "mServiceConnection2 onServiceConnected"); 23 24 25 try {26 mCustomServices. setDate (1999,); 27 mCustomServices. setTime (5, 45); 28} catch (RemoteException e) {29 e. printStackTrace (); 30} 31 32} 33 34 @ Override35 public void onServiceDisconnected (ComponentName componentName) {36 37} 38}; 39 40 41 @ Override42 protected void onDestroy () {43 super. onDestroy (); 44 unbindService (mServiceConnection); 45} 46}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.