I want to open activity via broadcast receiver , i tried any ways , but i am getting ActivityNotFoundException , my activity is working on normal mode, but when i want to open it from BroadCastRecevier it cause ActivityNotFoundException error,
It is my manifest ,
<activity
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:name="com.alexis.abc.ui.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>
And it is my broadcast receiver ,
Intent intent2 = new Intent(context, MainActivity.class);
intent2.addCategory("android.intent.category.LAUNCHER");
intent2.setAction("android.intent.action.MAIN");
context.startActivity(intent2);
Here is steps :
1 - I opening application and hiding launcher icon via following code
PackageManager packageManager = getContext().getPackageManager();
ComponentName componentName = new ComponentName(getContext(), MainActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
2 - I exiting to application and dialing a number (To triggering broadcast event) and i getting following exception
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.alexis.abc/com.alexis.abc.ui.MainActivity}; have you declared this activity in your AndroidManifest.xml?
Do you have a permission like this:
<receiver
android:name="Your receiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="action"/>
<category android:name="category" />
</intent-filter>
</receiver>
inside your Android Manifest file? If not, then try to open your Android Manifest file and put something like what I've posted above.
Or try .sendBroadcast() instead of .startActivity() on the last line of your code:
Intent intent2 = new Intent(context, MainActivity.class);
intent2.addCategory("android.intent.category.LAUNCHER");
intent2.setAction("android.intent.action.MAIN");
context.sendBroadcast(intent2);
Change your code which you want to open the application and hide the launcher icon:
ComponentName componentName = new ComponentName(getContext(), YourReceiver.class);
Don't forget to declare your BroadcastReceiver in manifest:
<receiver
android:name="YourReceiver" >
<intent-filter>
<!-- your intent filter goes here -->
</intent-filter>
</receiver>
Related
I am writing an application running on my custom device (marshmallow based). the general idea is to prompt a user dialog to enter some information at boot and send it to my server. once the information is successfully sent I update the shared preferences that the information was sent and the dialog will not be prompt anymore.
as always I check to see if the needed permissions are present (read phone state for IMEI and sim info), if not I create an activity that requests permissions and on result starts the user dialog activity. if permissions already exist I create the dialog activity straight from the boot receiver.
however, for some reason, sometimes the permissions activity is created twice (I made sure the boot receiver is only triggered once). this causes as expected the user dialog to be shown twice for no reason. does anyone know how can I solve this? thank you.
here is a code snippet for the boot receiver (the only place where the permissions activity is started).
public class BootReceiver extends BroadcastReceiver {
SharedPreferences mPrefs;
boolean wasUpdated;
#Override
public void onReceive(Context context, Intent intent) {
Log.v("car_num", "got boot receiver");
mPrefs = context.getSharedPreferences("car_num", MODE_PRIVATE);
wasUpdated = mPrefs.getBoolean("is_num_updated", false);
if (!wasUpdated) {
Log.v("car_num", "first time - asking for car num");
if (UtilityFunctions.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
Log.v("car_num", "we have permission lets ask for car num");
Intent i = new Intent();
i.setClassName("com.mgroup.carnumberapp", "com.mgroup.carnumberapp.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Log.v("car_num", "main activity started");
} else {
Log.v("car_num", "no permission - so requesting");
Intent i = new Intent();
i.setClassName("com.mgroup.carnumberapp", "com.mgroup.carnumberapp.PermissionActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("from", "boot receiver");
context.startActivity(i);
}
} else {
Log.v("car_num", "car number already updated - doing nothing");
}
}
}
edit:
I tried adding
android:launchMode = "singleTop"
however, it happened again.
here is my manifest :
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/MGServiceTheme">
<service
android:name=".SendDetailsService"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".PermissionActivity"
android:launchMode = "singleTop"/>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN"
android:theme="#style/Theme.AppCompat.NoActionBar" />
</intent-filter>
</activity>
</application>
In your manifest do android:launchMode="singleTop" for an activity
like
<activity android:name=".MapActivity"
android:launchMode="singleTop"/>
<activity android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I recently have added a layout and a java file connecting it to my java code file which is taking and giving actual output by implementing an algorithm but the second layout when opened in my phone it does not react to any of the two buttons clicked on it.
Seems like an error in AndroidManifest file.
here is Manifest file code,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplicationgeneric">
<application
android:allowBackup="true"
android:label="#string/app_name2"
android:icon="#mipmap/jug_black_trasnparent"
android:roundIcon="#mipmap/jug_black_trasnparent"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Next"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".connectorclass">
<intent-filter>
<action android:name="android.intent.action.ANSWER"/>
<category android:name="android.intent.category.Calculator"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
here connectorclass.java is linked to the Layout not Responding named something xyz..
Just remove the intent filter for "connectorclass".
In AndroidManifest.xml file, you provide two intent files. That doesn't need it. Always try to add one intent-filter on your activity and this activity is your lancer activity. So remove the second intent-filter from connectorclass activity.
Then add below code on your button onClickListener
Intent intent = new Intent(MainActivity.class, connectorclass.class);
startActivity(intent);
you can remove
<intent-filter>
<action android:name="android.intent.action.ANSWER"/>
<category android:name="android.intent.category.Calculator"/>
</intent-filter>
in android manifest and can start activity using class name by default
like this
Intent intent = new Intent(this, connectorclass.class);
startActivity(intent);
try this
I want to enable bluetooth with the following code from an intent service:
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
getApplicationContext().startActivity(enableBtIntent);
The logic around my application is:
Activity A launch Activity B which launch this ENABLE_REQUEST.
I tried to launch this request from a service and after even within Activity B itself, but Activity B is always destroy and Activity A comes foreground.
Here's the manifest.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ClassifierActivity"
android:screenOrientation="portrait"
/>
<service
android:name=".audio.SpeechRecognitionService"
android:exported="false"
android:description = "#string/speech_recognition_service_description"
/>
<service
android:name=".interaction.SmartObjectInteractionService"
android:exported="false"
/>
I couldn't find any solution!
P.S. Activity B has a Camera Fragment.
try using startActivityForResult instead of startActivity
I have two applications, the first one send an implicit intent to view a link and the second one named "MyBrowser" receives it. But when I install both apps on my phone (API level 23), the app chooser only display the Internet (default application for web viewing). If I use the AVD (API level 25), app chooser includes also "MyBrowser". Here is my first application's function:
private void startImplicitActivation() {
Log.i(TAG, "Entered startImplicitActivation()");
// Create a base intent for viewing a URL
Intent baseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
// Create a chooser intent, for choosing which Activity
// will carry out the baseIntent
Intent chooserIntent = Intent.createChooser(baseIntent, CHOOSER_TEXT);
// Verify the intent will resolve to at least one activity
if (baseIntent.resolveActivity(getPackageManager()) != null) {
Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());
// Start the chooser Activity, using the chooser intent
startActivity(chooserIntent);
}
And "MyBrowser" Manifest.xml:
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyBrowserActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
</intent-filter>
</activity>
</application>
I am newbie in android and can't understand why. Please explain to me. Thank you.
I have started an Intent and asked it to go to the main activity, when it attempts it the app crashes.
Here is the code that tries to go to the main activity.
Intent i = new Intent(
".MAIN_ACTIVITY");
startActivity(i);
Here is the XML manifest for Main_Activity.
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I'm still pretty new to this so any help and/or advice is of great value.
Write like this :
Intent i = new Intent(MainActivity.this, NewActivity.class);
startActivity(i);
Also you need to declare both activity class in manifest file like this:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".NewActivity"
android:label="#string/app_name" >
</activity>
For those, who come from Google, I was trying to pass large string in putExtra (over 90K Symbols) and my app was crashing because of that. The correct solution is either save the string to file or implement Singleton.
Here is the relevant link Maximum length of Intent putExtra method? (Force close)
as per your code: if i have create newActiviy in my project then:
i have to add that activity in android manifest file.
like:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<activity android:name=".newActivity"></activity>
</activity>
for calling that activity just do:
Intent intent = new Intent(MainActivity.this, newActivity.class);
startActivity(intent);
befre ask question here try some googling. and you must have to check this: Building Your First Android App and Starting Another Activity
Start new Activity like this:
Intent intent = new Intent(YourCurrentActivity.this, TargetActivity.class);
startActivity(intent);