Why an activity doesn't start? - java

I call an aidl method that calls me back after some logic, in this callback method I try to start an Activity, but the activity doesn't start and there are no exceptions. Interesting fact, it works on Android 9, but on 10 and 6 doesn't.
Manifest
<activity
android:name=".views.ReceiptsActivity"
android:launchMode="singleInstance"
android:windowSoftInputMode="stateHidden|adjustPan">
<intent-filter>
<action android:name="com.bifit.cashdesk.mobile.views.ReceiptsActivity" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
StartActivity() Method
private void showActivity() {
Intent intent = new Intent(context, context.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
context.startActivity(intent);
}
Aidl callback method
aidlService.cancel(new IOperationResponse.Stub() {
#Override
public void onResponse(String resultCode, String responseData, String rrn, String billJson) {
showActivity();
}
}, "com.bifit.cashdesk.mobile.views.ReceiptsActivity", rrn);
I have tried to resolve this by user-permissions, but it haven't led to success
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

It was on the side of aidl app, this app didn't close when used callback. We resolved it by closing aidl app on callback on it's side

Related

How can I send a String value to any particular app from another app without using ContentProvider or sharedPreference [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
appA mainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnSendBroadcast;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSendBroadcast = (Button) findViewById(R.id.btnSendBroadcast);
btnSendBroadcast.setOnClickListener(this);
}
#Override
public void onClick(View view) {
final Intent intent=new Intent();
intent.setAction("com.example.admin.chromium");
intent.putExtra("KeyName","code1id");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(
new ComponentName("com.example.admin.chromiumsendmessage","com.example.admin.chromiumsendmessage.MainActivity"));
sendBroadcast(intent);
Toast.makeText(getApplicationContext(), "KeyName value sent to ChromiumSendMessage app" , Toast.LENGTH_SHORT).show();
}
}
appB manifest--
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.chromiumsendmessage">
<uses-permission android:name="android.permission.SEND_SMS" />
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcast"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.admin.chromium" />
</intent-filter>
</receiver>
</application>
</manifest>
appB receiver class:
public class MyBroadcast extends BroadcastReceiver {
String msg;
String name;
#Override
public void onReceive(Context context, Intent intent) {
msg = intent.getStringExtra("KeyName");
name= msg;
Toast.makeText(context, "value - " + name, Toast.LENGTH_SHORT ).show();
}
}
AppB manifest appA mainActivity
There are two Applications, appA & appB. In appA there is a button. In appA, if I click on the button it should send some value (String/Integer) to the appB without the use of ContentProvider or SharedPreference. Moreover, the appB should receiver the value in its' BroadcastReceiver class. Could anyone help me please?
I have added appA MainActivity code and Receiver class and Manifest from appB.
You can use sendBroadcast() method to send String value from one app to another.
However, first of all you need to register your receiver app in Manifest.
For instance, suppose you have two apps, App1 and App2.
App1 will send the String value and App2 will receive that value.
In your App1, code should be like this,
final Intent intent=new Intent();
intent.setAction("com.pkg.App1");
intent.putExtra("KeyName","code1id");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(
new ComponentName("com.pkg.App2","com.pkg.App2.MyBroadcastReceiver"));
sendBroadcast(intent);
To receive this broadcast message in App2, write code in Manifest of your App2 following below:
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.pkg.App1" />
</intent-filter>
</receiver>
FLAG_INCLUDE_STOPPED_PACKAGES flag is added to the intent before it is
sent to indicate that the intent is to be allowed to start a component
of a stopped application.

Unable to start receiver (Activity Not Found)

I developed an application and put that on app store. Afterwords i wanted to change the package name so i just changed the application ID in build.gradle so that it looks appropriate in link. I didn't change anything else not the package name, not the manifest file etc. Application worked fine but now it's showing an error of ActivityNotFound exception on the launcher activity which is called through a broadcast receiver although that activity is defined in manifest file. May i know where am i wrong at?
This is the manifest file coding:
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
<activity
android:name=".BatteryChargerFast"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And below is the coding of broadcast Receiver:
public class PowerConnectionReceiver extends BroadcastReceiver {
private String TAG="PowerConnectionReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("packagename",
"packagename.BatteryChargerFast");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("fast", true);
context.startActivity(i);
}
}
the error states:
java.lang.RuntimeException: Unable to start receiver
packagename.PowerConnectionReceiver:
android.content.ActivityNotFoundException: Unable to find explicit
activity class {packagename/packagename.BatteryChargerFast}; have you
declared this activity in your AndroidManifest.xml?
When you change applicationId in gradle, it overrides the Manifest's id.
so you need to change your code from:
Intent i = new Intent();
i.setClassName("packagename",
"packagename.BatteryChargerFast");
To:
Intent i = new Intent();
i.setClassName("your.new.app.id",
"packagename.BatteryChargerFast");
or may be even simpler where you don't need to consider all this:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context.getApplicationContext(), BatteryChargerFast.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("fast", true);
context.startActivity(i);
}

BroadcastReceiver not receiving download complete action

I am trying to capture download complete events, but my BroadcastReceiver is not receiving them. Here is the receiver:
public class DownloadListenerService extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
System.out.println("got here");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
String downloadPath = intent.getStringExtra(DownloadManager.COLUMN_URI);
editor.putString("downloadPath", downloadPath);
editor.commit();
}
}
}
Here is the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name="com.example.alreadydownloaded.DownloadListenerService"
android:exported="true">
<intent-filter>
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
</application>
Anyone see what's wrong?
Use full package name for you receiver like com.example.DownloadListenerService
Add android:exported="true" BroadcastReceiver can receive messages from sources outside its application.
Change the name of the Action in the intent-filter to android.intent.action.DOWNLOAD_COMPLETE
<receiver
android:name="com.example.DownloadListenerService"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.INTERNET" />
The receiver only will be triggered if was registered from your application using registerReceiver(#Nullable BroadcastReceiver receiver,IntentFilter filter);
Code to enqueue Download :
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.google.com/images/srpr/logo4w.png"));
dm.enqueue(request);
I think the action name in your XML is wrong. The docs state that the correct one is: android.intent.action.DOWNLOAD_COMPLETE not DownloadManager.ACTION_DOWNLOAD_COMPLETE - you need to use the constant, not the Java form.
<receiver android:name=".DownloadListenerService" >
<intent-filter>
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
<receiver
android:name=".BroadCast_Service.Download_BroadCast"
android:exported="true">
<intent-filter android:priority="1099">
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
I think you are calling DownloadManger service from the IntentService/Service. If so remove it from there and put it into activity.
add permission in android manifest
<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
If the download is based on your app then you need to send a broadcast?
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
sendBroadcast(i);

Android-Launching an application when tablet boot up is completed

i am using Android 3.2, i created my android application and that will start automatically when my tablet start booting is completed.
Everything works fine except the activity is running twice when apps is start running automatically (after boot completed).
This problem is not raised when i start the app manually.
public class BootStartUpApp extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startUpApps = new Intent(context, StartMainActivity.class);
startUpApps.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startUpApps);
}
}
in Manifest file i added below code :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.logica.eHealthBox.tab.activity.BootStartUpApp" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
here's how to do it:
public class BootReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, TestReceiversActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
put an entry in the manifest:
<receiver android:name=".BootReceiver" />
and have the activity too.
for more info , check this link and this link

Home button listener in background

I want to create service (?), which would be running in the background all the time and start when system starts. Also, I want it to detect double home button press and then launch a specified activity. Is it possible?
1. When system starts you can do like this:
public class PhoneStateReceiver extends BroadcastReceiver{
#Override
public void onReceive(final Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent launch = new Intent(context, ServiceToLaunch.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(launch);
}
}
}
In your manifest add this:
<receiver android:name=".receiver.PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Add permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2. There is no Home key event available.

Categories