I want to make a broadcast and my Receiver should receive the broadcast, but it is not working.
I have the following code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="utilities.dip.com.checkbattlevelstackof">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
android:name=".YourService"
android:enabled="true"
android:exported="false"
android:label="#string/app_name" >
</service>
<!-- Receivers -->
<receiver
android:name=".AlarmReceiver"
android:enabled="true" />
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The receiver is:
package utilities.dip.com.checkbattlevelstackof;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver {
public static final String TAG = "BootReceiver";
public static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(ACTION_BOOT)) {
// This intent action can only be set by the Android system after a boot
Log.d(TAG,"Received boot event");
Intent monitorIntent = new Intent(context, YourService.class);
monitorIntent.putExtra(YourService.HANDLE_REBOOT, true);
context.startService(monitorIntent);
}
else{
Log.d(TAG," Action received : " + intent.getAction());
}
}
}
When I am making a broadcast I am not getting any log:
platform-tools $ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.DEFAULT -n utilities.dip.com.checkbattlevelstackof/utilities.dip.com.checkbattlevelstackof.BootReceiver
Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED cat=[android.intent.category.DEFAULT] cmp=utilities.dip.com.checkbattlevelstackof/.BootReceiver }
Broadcast completed: result=0
What is the mistake ?
That's because you have declared outside in your manifest. It basically should look like this:
<application
.....
<receiver
android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
Also while checking the action string especially for the default Android actions, don't use your own constant. Instead of using ACTION_BOOT constant use "Intent.ACTION_BOOT_COMPLETED"
Add receiver & service inside application tag.
Related
I want my rn app autostart when boot up, but it seems that my app cannot receive the boot completed broadcast. Is there anything wrong with my code?
I've already add all the user permissions(uses-permission and action), and make sure my apk not in sdcard, not in stopped state...
here is my code:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rnproject"
android:installLocation="internalOnly"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<receiver
android:name=".com.rnproject.BootBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file">
</data>
</intent-filter>
</receiver>
</application>
</manifest>
BootBroadcastReceiver.java
package com.rnproject;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootBroadcastReceiver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BootBroadcastReceiver", intent.getAction());
if (intent.getAction().equals(ACTION)) {
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
}
MainActivity.java
package com.rnproject;
public class MainActivity {
public static void main(String[], args) {
System.out.print("HelloWorld");
}
}
I expect my app can receive the system broadcast, if you can serve my problem, thanks in advance!
i have code in file manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ninhph.btvncallblock">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<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=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.AddActivity" />
<receiver android:name=".receiver.CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
and in broadcastReceiver:
package com.ninhph.btvncallblock.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("call", "call coming: ");
}
}
But when i run and call in emulator, nothing happen, why? and how can i detect incoming call?
add <uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS"/> permission in AndroidManifest.xml also try set priority to intent filter like below code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ninhph.btvncallblock">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<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=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.AddActivity" />
<receiver android:name=".receiver.CallReceiver">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
in receiver code
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("call", "call coming: ");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Hey! Calling Number : " + incomingNumber, Toast.LENGTH_LONG).show();
}
}
}
I want to start an activity even before screen lock after rebooting. here is my code
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.bootservicestartup.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>
<receiver android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
public class BootUpReceiver extends BroadcastReceiver {
#SuppressLint("InlinedApi")
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, MainActivity.class);
i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
what the code does is when the phone is rebooted, an activity will be shown. that works. but I also want the activity to be shown even before showing the screen lock.
Hope this helps you if you did it as a service;
You need the following in your AndroidManifest.xml file:
1) In your element:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
(you don't need the android:enabled, exported, etc., attributes... the Android defaults are correct)
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
How do i set my application as startup application, so when ever mobile restarts or turned ON, my application starts.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.installedapps22"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application android:icon="#drawable/cherry_icon" android:label="#string/app_name">
<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>
<activity android:name=".ListInstalledApps" > </activity>
<activity android:name=".TabsLayoutActivity" />
</application>
</manifest>
EDIT Here is my updated code and it is still not working:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.installedapps22"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/cherry_icon" android:label="#string/app_name">
<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>
<receiver android:enabled="true" android:name="com.app.reciever.BootUpReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".ListInstalledApps" > </activity>
<activity android:name=".TabsLayoutActivity" />
</application>
</manifest>
BroadcastReciever:
package com.example.installedapps22;
public class BootUpReciever extends BroadcastReceiver
{
#Override
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
This is to set the app as the startup application in your device Create a Class extends BroadCast Reciever
public class BootUpReciever extends BroadcastReceiver
{
#Override
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent(context, ServerPreferenceActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Add permissions to manifest file to access bootup receiver
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Register your receiver which extended the Broadcast receiver in manifest.xml
<receiver android:enabled="true" android:name="com.app.reciever.BootUpReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
first use permission in manifiest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and declare boot receiver in manifiest
<receiver android:name=".BootReciever">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
user receiver to start your mainactivity
public class BootReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
For your edited code try replacing the following:
android:name="com.app.reciever.BootUpReciever
with:
android:name="com.example.installedapps22.BootUpReciever
I am testing on an physical device (SAMSUNG ACE GT S5830i)
But I am not receiving the BOOT_COMPLETED intent therefore the service is not Receiver is not starting
This is the code I am using.
public class BootCompleteReceiver extends BroadcastReceiver {
static final String TAG = "InfoService.BroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e(TAG, "System boot notification received.");
Intent service = new Intent(context, InfoService.class);
context.startService(service);
} else {
Log.e(TAG, "Intent received: " + intent);
}
}
}
This is the Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appengine.paranoid_android.lost"
android:versionCode="2"
android:versionName="1.1">
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity android:name=".InfoSetup"
android:label="#string/activity_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LockScreen"
android:label="#string/activity_name"
android:launchMode="singleInstance"
android:clearTaskOnLaunch="true"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<service android:name=".InfoService"
android:label="#string/service_name"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_CHANGED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<!-- <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> -->
Try to add the full path with the package for <receiver android:name=".BootCompleteReceiver">
<receiver android:name="com.appengine.paranoid_android.lost.BootCompleteReceiver">