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.
Related
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
i add android.intent.action.PACKAGE_FULLY_REMOVED as action
and this my receiver
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_FULLY_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
and my MyBroadcastReceiver here
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"app Removed",Toast.LENGTH_LONG).show();
Log.e("MyBroadcastReceiver","app Removed");
}
}
so i want to notify when app is removed from Device but is not working
You can't listen to android.intent.action.PACKAGE_FULLY_REMOVED/android.intent.action.PACKAGE_REMOVED on your all package because your own BroadcastReceiver is removed when your package is removed.
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.
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);
}
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