I've a reciever
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myStarterIntent = new Intent(context, MainActivity.class);
context.startActivity(myStarterIntent);
}
}
and have modified the AndroidManifest.xml, adding these lines
<receiver
android:enabled="true"
android:name=".MyBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
to section.
The application still does not start on system boot..any ideas appreciated. At least how can I monitor what is going on after device reboot (because I cant just use breakpoints in that case)
You need to provide Intent.FLAG_ACTIVITY_NEW_TASK flag when starting activity from BroadcastReceiver.
Intent myStarterIntent = new Intent(context, MainActivity.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
This is how you can test the BroadcastReceiver.
Related
with this How do I start my app on startup? post run app when devise power on success
but when devise run this show my main_activity
i want run app in background (Because i call a volley request with timer for check server in background )
my manifest.xml :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:enabled="true"
android:exported="true"
android:name="other_class.CLASS_START_UP"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
my class start_up :
public class CLASS_START_UP extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
i use this post Run volley request every 5 minutes in background android to run volley request in timer
my timer code is :
private Runnable runnableCode = new Runnable() {
#Override
public void run() {
get_start_info();
// Repeat this the same runnable code block again another 2 seconds
handler.postDelayed(runnableCode, 10000);
}
};
my problem is i dont want when devise turn on show main activity show to user (foreground) i want only run in background
i am sorry i am not good in English language
Try This one
<receiver android:name=".service.ShutdownReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
ShutdownReciver.java
public class ShutdownReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("Brodcast Call","####");
Toast.makeText(context, "service", Toast.LENGTH_SHORT).show();
// context.startService(new Intent(context,
PowerButtonService.class));
Intent myIntent = new Intent(context, PowerButtonService.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
recently I use BOOT_COMPLETED 2 app (A app, and B app)
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
A app is activity and B app is service app
when my device boot,
first A app launch and B app launch
so, show B app screen.
I want
first B app launch and A app launch showing A app screen
perhaps, Can I give BOOT_COMPLETED Priority is possible?
finally, I want when I boot my device, show A app screen
Thanks!
add
I try
B app(service)
public class BootCompletedReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) {
Intent i = new Intent("A app package name.BOOT_COMPLETED");
context.sendBroadcast(i);
}
}
}
<receiver android:name=".BootCompletedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
A app(activity)
public class BootSendReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context,Intent intent) {
if( intent.getAction().equals("B app packagename.BOOT_COMPLETED"));
Intent i = new Intent (context, MainActivity.class);
context.startActivity(i);
}
}
<receiver android:name=".BootSendReceiver">
<intent-filter>
<action android:name="blackeyeonandroid.iosystem.co.kr.simpleserviceexample.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
and I try boot .. but showing B app screen
I think you can not control that..
Instead, you can make APP1 start the APP2. This way, only APP1 receives the BOOT_COMPLETE message. Then, APP1 is responsible to send a new intent to start APP2:
Maybe, you can do as follows (note that APP2 does not receive android's default BOOT_COMPLETED message):
APP1
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".AppToStartFirstBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Receiver
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent("com.example.mytestapp.BOOT_COMPLETED");
context.sendBroadcast(i);
}
}
APP2
Manifest:
<receiver android:name=".AppToStartLaterBroadcastReceiver">
<intent-filter>
<action android:name="com.example.mytestapp.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Receiver
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.example.mytestapp.BOOT_COMPLETED")) {
// Do what you want in secundary APP
}
}
Note
This is an suggestion and you should adjust to your case. Since I don't have more details about your code, you may need to modify it to your case.. But you can use the idea.
I want to develop app that doesn't have icon launcher. The app will run alarmscheduler which triggered when app is installed or phone is rebooted.
The problem is how can I open the activity since the app doesn't have intent filter like below:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Is there a way to handle dial code such as ##4635*#*# to open the activity ?
or any other solutions are welcomed.
You can do it with two ways:
1) as answer by #KishuDroid
2) By define code in manifest
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
In manifest file
<receiver android:name=".MySecretCodeReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="4635" />
</intent-filter>
</receiver>
Note:
In Second method you must have to dial *#*#your_code#*#* and dont need to press call button
But in first method you can customise your prefix or postfix of code. For example *#your_code# or **your_code##. but you need to press call button.
You have to use Broadcast Receiver...
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
if(code.equals("#056700") {
intent.setComponent(new ComponentName("com.example", "com.example.yourActivity"));
And Your Android Manifest
<receiver android:name="com.varma.samples.detectcalls.receivers.OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
Also, include the permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
I have music service that playing audio files. I start this service from my activity. I send notification with play/pause button when i click on play button from notification i send broadcast receiver and it's working. In on onReceive(Context context, Intent intent) method i call context.startService(intentService) the problem is MusicService.onStartCommand() never called.
My receiver
public class PlayEpisodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Service Started", Toast.LENGTH_SHORT).show();
if(MusicService.isRunning()) {
Intent serviceIntent = new Intent(context, MusicService.class);
serviceIntent.putExtra(AppConstants.ACTION_TYPE, AppConstants.ACTION_PLAY);
context.startService(intent);
}
}
}
My manifest:
<service
android:enabled="true"
android:name=".backgroundtasks.MusicService">
<intent-filter>
<action
android:name = "cc.gm.oscarradio.backgroundtasks.MusicService">
</action>
</intent-filter>
</service>
<receiver
android:enabled="true"
android:name=".Receivers.PlayEpisodeReceiver">
<intent-filter>
<action android:name = "cc.gm.oscarradio.ACTION_MUSIC"/>
</intent-filter>
</receiver>
In your code there should be a slight change I feel, you should replace the following line:
context.startService(intent);
by
context.startService(serviceIntent);
Hope this helps you.
I am trying to launch the main activity (which is the only activity) named as "MainActivity" when the Android OS starts. The app is installed on the internal storage but it says Unfortunately, The app(Name of app) has stopped. I am using the Boot Receive code as below
My MainActivity code in .java file
public class BootUpReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Finally, did the right thing after searching some time, For some one who is stuck up here. What you have to do is register the onReceive function in a separate package, what i was getting the error in Logcat was Classnotfound as i placed the code in my MainActivity class and its package. As soon as i did changed the onReceive function to new package the application worked flawlessly.
try this:
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
https://stackoverflow.com/a/11079306/6237295