Enable communication between two Android applications using user permissions

One of my Android apps requires some features that are “dangerous” when it comes to permissions. They require some permissions, such as Internet access, which are crucial when combined with private data.

That's why I want to create a separate “add-in,” that is, a second application that provides these critical functions. Therefore, if users want them, they can install the add-in, but the main application will work without these permissions.

Using sharedUserId will obviously be the easiest solution, but adding it later, when many users will use the application already, can cause serious problems. Does this not mean that the application will no longer be able to access its own data?

Therefore, I have to choose a different approach. ContentProviders is something that I try to avoid because they are too complex for this simple need, in my opinion.

I thought user permissions could solve the problem. They can? I added the following permission declaration for both the main application and the add-in as a child of the manifest tag in AndroidManifest.xml :

 <permission android:name="com.my.package.ADDON" android:label="@string/permission_title" android:description="@string/permission_description" android:permissionGroup="android.permission-group.PERSONAL_INFO" android:protectionLevel="signature" /> 

In addition, both manifest files received this part:

 <uses-permission android:name="com.my.package.ADDON"></uses-permission> 

The add-on application includes IntentService , which has the following attribute:

 android:permission="com.my.package.ADDON" 

Shouldn't this be done so that I can call the IntentService add- IntentService from my main application through this code?

 Intent addonIntent = new Intent(); addonIntent.setClassName("com.my.package", "com.my.package.MyService"); startService(addonIntent); 

Unfortunately, this call always fails with the following exception:

 E/AndroidRuntime(16721): java.lang.SecurityException: Not allowed to start service Intent { cmp=com.mypackage.addon/.MyService } without permission com.mypackage.permission.ADDON 

What I did wrong? Thank you so much in advance!

Addition # 1 - Add-on manifest:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.mypackage.addon"> <uses-sdk android:minSdkVersion="8" /> <permission android:name="com.mypackage.permission.ADDON" android:label="@string/permission_title" android:description="@string/permission_description" android:permissionGroup="android.permission-group.PERSONAL_INFO" android:protectionLevel="signature" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:permission="com.mypackage.permission.ADDON" android:exported="true"> <service android:enabled="true" android:name=".MyService" /> </application> </manifest> 

Addendum # 2 - manifest of the main application:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.mypackage.mainapp"> <uses-sdk android:minSdkVersion="8" /> <permission android:name="com.mypackage.permission.ADDON" android:label="@string/permission_title" android:description="@string/permission_description" android:permissionGroup="android.permission-group.PERSONAL_INFO" android:protectionLevel="signature" /> <uses-permission android:name="com.mypackage.permission.ADDON"></uses-permission> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name="MyApp"> <activity android:name=".MainActivity" android:launchMode="singleTask" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 
+4
source share
1 answer

Does this mean that the application will no longer be able to access its own data?

Right.

I added the following permission

I would reset permission-group as it is not necessary.

In addition, both manifest files have received this part now

You may only need the one who called your IntentService .

Shouldn't this be done so that I can call the IntentService add-in from my main application through this code?

Not if IntentService not exported. Make sure your IntentService has either <intent-filter> or android:exported="true" . I would recommend following the <intent-filter> route, so you can declare and use a custom action bar so that you get away from hard coding and class names in the client application.

Here is a directory with two sample projects using this basic approach, although in my case the messages are based on a reliable ContentProvider than a protected IntentService . The concept is the same, and therefore, with these small changes, I would expect that you do to work very well.

+5
source

All Articles