Custom permission in Android - android

I'm trying to figure out how to work with custom permissions in Android.
https://developer.android.com/guide/topics/manifest/manifest-intro.html#perms .
I found similar problems answered here but none of the answers worked for me...
I've created simple app with Android studio 2.1.1
package com.example.lukas.permtest;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
with manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lukas.permtest">
<permission android:name="com.example.lukas.permission.ACTIVITY_PERM"
android:label="#string/permlab_activity"
android:description="#string/permdesc_activity"
android:protectionLevel="normal"/>
<uses-permission android:name="com.example.lukas.permission.ACTIVITY_PERM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:permission="com.example.lukas.permission.ACTIVITY_PERM">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I have Sony Xperia Z2 tablet with Andoid 4.4.2 and Xperia Z3+ With Android 6.0
The app doesn't work. I get the same exception for both devices...
W/ActivityManager: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.lukas.permtest/.MainActivity } from ProcessRecord{44cf1178 2461:com.sonyericsson.home/u0a95} (pid=2461, uid=10095) requires com.example.lukas.permission.ACTIVITY_PERM
What am I doing wrong?
Thanks

Hah! That's pretty funny!
I believe that what is going on here is that, although you've done everything right as far as your process and the permission, you forgot one small thing...
The Launcher needs to start your app. The Launcher doesn't have your custom permission.
In other words, your application could launch its own MainActivity, because it has the permission to do so. The Launcher does not have the permission, so when it attempts to launch the android.intent.action.MAIN Activity, if fails for lack of permission.
Separate the Main Activity from the Activity protected by the permission. Launch the protected Activity from the Main Activity and everything go smooth as silk.

Related

Defining your own permissions in android

I am trying make my own permission for android application.
For this my android manifest file looks like this.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.happybirthday" >
<permission
android:name="com.example.hp.happybirthday.PERM"
android:description="#string/pdesc"
android:label="#string/CAREFUL"
/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:permission="com.example.hp.happybirthday.PERM">
<activity
android:name=".MainActivity"
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>
The problem that I am facing is that I want to have my own permission associated with this application and hence i have added the following line as shown above under the application tag, so that only those activities have access to this app which possess my defined permission.
android:permission="com.example.hp.happybirthday.PERM"
But the problem is that when I try to run my app, the app whose manifest file I have declared, it shows the error app is not installed.
But when I remove the above mentioned line, it works, but then any activity will have access to this app which I do not want.
when I try to run my app, the app whose manifest file I have declared, it shows the error app is not installed
That is because the home screen is an app, and the home screen does not hold your custom permission. Hence, the home screen cannot start your launcher activity.
then any activity will have access to this app which I do not want
First, custom permissions do not work all that well.
Second, permissions are usually applied at a finer granularity than "this app". You only secure those components that need the security, and you leave public other components, like the launcher activity.
It looks like you've defined the permission, and set it to be required... but you haven't actually granted it to your own app. Add a uses-permission tag

Android - Issues starting the correct activity?

Ok guys I'm super new at this, so bear with me...I'm basically reading a book on Android development, and following a tutorial in it.
This app has 7 activities (1 called QuizActivity that extends Activity, and 6 others that extend QuizActivity - 1 of which is QuizSplashActivity, the one I want to launch on startup)
However, I'm super confused as to why the default activity doesn't seem to be launching. My manifest has the correct tags for QuizSplashActivity, and QuizSplashActivity points to the correct .xml layout file I created. When I run the program, however, the console says:
[2013-03-11 17:19:47 - BeenThereDoneThat] Starting activity com.example.beentheredonethat.QuizActivity on device emulator-5554
[2013-03-11 17:19:48 - BeenThereDoneThat] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.beentheredonethat/.QuizActivity }
Here is the application section of the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="QuizSplashActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="QuizActivity"></activity>
<activity android:name="QuizGameActivity"></activity>
<activity android:name="QuizHelpActivity"></activity>
<activity android:name="QuizMenuActivity"></activity>
<activity android:name="QuizScoresActivity"></activity>
<activity android:name="QuizSettingsActivity"></activity>
</application>
And here is my QuizSplashActivity, that I want to show on startup:
package com.example.beentheredonethat;
import android.os.Bundle;
import android.view.Menu;
public class QuizSplashActivity extends QuizActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
}
Any ideas as to why this is doing this? Any help would be greatly appreciated. Thanks!
I think that problem is your in manifest in declaring Activities. You are missing dot before Activity name:
android:name=".QuizSplashActivity"
and compilator can't find your Activity. Try to fix it and it should works. Also try to clean your project, restart Eclipse and try to start app again.

Phonegap Android app restarting instead of resuming

this question was asked 3 times here but no answer.
I´m using Phonegap 2.1 on android device 2.3.6 (Galaxy SII)
I´m also using AdMob Phonegap plugin.
Well the problem is the same as stated by others:
When trying to resume thru the app icon the app is restarted even though it´s in memory
When passing thru the task manager it is resumed normally
My main activity
public class testApp extends DroidGap
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
setIntegerProperty("loadUrlTimeoutValue", 60000);
super.loadUrl("file:///android_asset/www/index.html",5000);
}
}
manifest.xml
<application android:icon="#drawable/icon" android:label="#string/app_name"
>
<activity android:name="testApp" android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
/>
</application>
Thank you for any advice
If you have "Do not keep activities" selected in the Developer Options section in the Settings app, then no matter what you configure your manifest to, all activities will be destroyed when not in use by the user. This applies to PhoneGap apps and native apps.
place android:launchMode="standard" in manifest for testApp Activity

Difference between android 2.3.3 and android 4.2 enforcing permissions?

I am just playing with a certain scenario of inter-application-communication,
and trying to circumvent the problem that in Android the main activity of an application
can't be secured by a custom permisssion (as in that case it can't be launched at all).
I tried to use two activities instead: 1) The main activity which is not protected.
2) When a button is clicked, the main activity sends an explicit Intent to start the second activity.
That one performs some sensitive work and is protected by a custom permission
("toy.test.permission.ACTIVATE_SECOND_ACTIVITY").
The idea being that if the main activity is either started by the user from the launcher or
by a foreign maliscious application using an explicit intent, we can warn the user before he pushes the button to proceed.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
final Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent activateIntent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(activateIntent);
finish();
}
}); ....
The Manifest.xml file looks like that:
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/second_activity_name"
android:theme="#style/AppTheme"
android:permission="toy.test.permission.ACTIVATE_SECOND_ACTIVITY" >
<intent-filter>
<action android:name="toy.test.action.ACTIVATE_SECOND_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
(In addition, the SecondActivity can be started from a friend application by an implicit Intent with
actionString="toy.test.action.ACTIVATE_SECOND_ACTIVITY" using the permission
"toy.test.permission.ACTIVATE_SECOND_ACTIVITY".)
Now the difference between android 2.3.3 and android 4.2:
On an android 4.2 device or emulator, the SecondActivity can be started from the MainActivity by clicking
the Start button without any problems, although the application does not ask explicitly for the permission
"toy.test.permission.ACTIVATE_SECOND_ACTIVITY".
However, on an android 2.3.3 emulator, the SecondActivity can't be started because of SecurityException:
FATAL EXCEPTION: main
java.lang.SecurityException: Permission Denial: starting Intent { cmp=toy.test/.SecondActivity }
from ProcessRecord{406827d0 405:toy.test/10034} (pid=405, uid=10034) requires
toy.test.permission.ACTIVATE_SECOND_ACTIVITY
I am confused: Was the permission enforcement changed between the two Android versions?
And is it on purpose that android 4.2 allows such situations, i.e. activation of the SecondActivity without permission?
(Of course, that would be useful.)
Thanks a lot for any answers,
puffin137

Any fixes for crash because of combination of WRITE_SETTINGS and SEARCH_LONG_PRESS?

I'm trying to create an app that will change a setting when the user holds the search button. It seems that the combination of the WRITE_SETTINGS permission and the fact that the activity is launched from another application using the long press makes the parent task crash. For example, if I'm looking at Google Maps and hold the search button, the SearchButtonPopup does not appear and Google Maps crashes.
I tried taking the Theme.Dialog out, but it didn't make any difference. Removing the permission makes it robust again, no crashes, but I need the permission! Any fixes or workarounds?
These are my manifest and java files:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.stathissideris.searchbuttonpopup" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application android:icon="#drawable/logo" android:label="#string/app_name"
android:debuggable="true" android:permission="android.permission.WRITE_SETTINGS">
<activity android:name=".SearchButton" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.SEARCH_LONG_PRESS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
SearchButton.java
package org.stathissideris.searchbuttonpopup;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class SearchButton extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("SearchButtonPopup", "search held");
}
}

Categories

Resources