How to add accesspermission to a Service

Source: Internet
Author: User
First, define the Service interface, IMyService. aidlpackagecom. xxxx; importandroid. OS. IBinder; importandroid. OS. ParcelFileDescriptor; interfaceIMyService {voiddoService (intid);} implements the service class, MyService. Example

// First define the Service interface, IMyService. aidl package com. xxxx; import android. OS. IBinder; import android. OS. parcelFileDescriptor; interface IMyService {void doService (int id);} // implements the service class, MyService. java public class MyService extends IM

// First define the Service interface, IMyService. aidl
Package com. xxxx;

Import android. OS. IBinder;
Import android. OS. ParcelFileDescriptor;

Interface IMyService {
Void doService (int id );
}
// Implement the service class, MyService. java
Public class MyService extends IMyService. Stub {
Public static final String MY_SERVICE = "myservice ";
Private static MyService sService = null;
Static String PERMISSION = "com. xxxx. permission. ACCESS_MYSERVICE ";

/*
* The entry called by system server to create service.
*/
Public static MyService main (Context context ){
If (sService! = Null ){
Return sService;
}
SService = new MyService (context );
Try {
Slog. d (TAG, "created service ");
ServiceManager. addService (MY_SERVICE, sService );
Slog. d (TAG, "added service" + MY_SERVICE );
} Catch (Throwable e ){
Slog. e (TAG, "Failure starting MyService", e );
}
Return sService;
}

Private MyService (Context context ){
MContext = context;
}

/*
* Called from Client App to retrieve interface
*/
Public static IMyService getService (){
IBinder B = ServiceManager. getService (MY_SERVICE );
If (B = null ){
Return null;
}
Return IMyService. Stub. asInterface (B );
}

@ Override

Public long openSession (IBinder clientToken, int sensorType) throws RemoteException {

// Add Access Permissions

If (mContext. checkCallingPermission (PERMISSION )! = PackageManager. PERMISSION_GRANTED ){
Throw new RemoteException ("Permission not granted for MyService ");
}
// Do the actual work

}

}

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.