I've installed a broadcast receiver a few weeks ago in Java, now I want to do it in Kotlin, but unfortunately it does not work.
My Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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="com.test.test.BootReceiver"
android:enabled="true"
android:exported="true"
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>
My receiver:
package com.test.test
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.Intent.ACTION_BOOT_COMPLETED
import android.util.Log
class BootReceiver : BroadcastReceiver() {
override fun onReceive(c: Context, intent: Intent?) {
val action = intent?.action
Log.d("ASD123123", "RECEIVED BOOT")
val b = intent?.toUri(Intent.URI_INTENT_SCHEME)
when (action) {
ACTION_BOOT_COMPLETED -> startWork(c)
}
}
private fun startWork(context: Context) {
Log.d("Test", "Test")
}
}
When I reboot my device (Android 8.0.0) it does not start the service. Neither does it when I run
adb shell
am broadcast -a android.intent.action.BOOT_COMPLETED
With
am broadcast -a android.intent.action.BOOT_COMPLETED -n com.test.test/.Bootreceiver
it works though. What is wrong with my code?
BOOT_COMPLETED broadcast receiver has been deprecated and no longer works.
<receiver android:name="com.example.startuptest.StartUpBootReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" /> // add this line
</intent-filter>
And in Broadcast receiver :
private fun startWork(context: Context) {
Log.d("Test", "Test") // remove this and toast
Toast(context,"Test", Toast.LENGTH_SHORT).show()
}
Related
I'm rebooting the phone and I expect a log message. I've registered the BroadcastReceiver in Androidmanifest, but no message is being received. What am I Missing?
androidmanifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Receives an event when the device has completed a reboot -->
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.BOOT" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
BootReceiver:
class BootReceiver : BroadcastReceiver() {
override fun onReceive(p0: Context, p1: Intent) {
Log.i("BootReceiver", "Boot event received")
}
}
When I filter in the logging for "boot" I see several other apps logging the event, but not my own App:
I/NU.LockBootCompleteReceiver: onReceive : Intent { act=android.intent.action.LOCKED_BOOT_COMPLETED flg=0x89000010 cmp=com.samsung.android.app.telephonyui/.netsettings.ui.receiver.LockBootCompleteReceiver (has extras) }
I/BCL#CoreSvc: (CoreServiceComponentEnabler) updateComponent() : class com.samsung.android.bixby.receiver.token.BootCompleteReceiver | 2
I/BCL#CoreSvc: (CoreServiceComponentEnabler) updateComponent() : class com.samsung.android.bixby.receiver.token.LazyBootCompleteReceiver | 1
I/CIDManager: [onReceive(BootReceiver.java:20)] onReceive: android.intent.action.LOCKED_BOOT_COMPLETED
I/ActivityManager: Start proc 7201:com.samsung.android.game.gametools/u0a79 for broadcast {com.samsung.android.game.gametools/com.samsung.android.game.gametools.floatingui.receiver.GameBoosterBootCompleteReceiver}
I/GameTools: BootCompleteReceiver already enabled.
I/GameTools: GameBoosterBootCompleteReceiver: onReceive: Intent.ACTION_LOCKED_BOOT_COMPLETED
I/GameTools: GameBoosterBootCompleteReceiver: clear runtime settings on boot complete.
I/GameTools: GameBoosterBootCompleteReceiver: register Intent.ACTION_USER_UNLOCKED
I/GameTools: GameBoosterBootCompleteReceiver: register Intent.ACTION_USER_UNLOCKED via EventDelegationManager
I/ORC/FbeBootReceiver: onReceive : android.intent.action.LOCKED_BOOT_COMPLETED
I/ORC/FbeBootReceiver: FBE islocked : true
I/CS/MsgFMMReceiverService: PCW LOCK. handlePCWLockMessage. LockedBootComplete : true
I/CS/xmsFbeJobService: onLockedBootCompleted()
I/ORC/FbeMigrationJobService: onLockedBootCompleted()
I/StateUtils: isDirectBootMode On : true
I/PackageManager: !#Start postBootUpdate
I/PackageManager: !#Finish postBootUpdate dexopted: 3
Remove:
<category android:name="android.intent.category.DEFAULT"/>
Categories are normally only used on activities, not broadcasts.
The code above is working fine.
I have checked on device ->
google pixel 4a, os 12
mi a1, os 11
i am able to see logs every time i reboot my device.
Try changing your device and test.
check the manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.BOOT" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>
I am trying to code a service that started when device booted.
java.lang.RuntimeException: Unable to start receiver packagename.BootReciever: android.content.ActivityNotFoundException: Unable to find explicit activity class {packagename/packagename.MainActivity$Companion}; have you declared this activity in your AndroidManifest.xml?
In my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="packagename">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INJECT_EVENTS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Translucent"
android:fullBackupContent="#xml/backup_descriptor">
<activity android:name="packagename.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="packagename.RemoteService">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</service>
<receiver android:name="packagename.BootReciever"
android:enabled="true"
android:exported="true">
<intent-filter android:directBootAware="true">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
</application>
</manifest>
When i open app, it will work well, however i want to launch application when boot completed. I push it into system/app and it crushes.
override fun onReceive(p0: Context?, p1: Intent?) {
if(p1?.action.equals("android.intent.action.BOOT_COMPLETED")){
Log.e("Ahmet ","START PLSSSSSSSSSSSSS")
val startIntent = Intent(p0,MainActivity.javaClass)
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
p0?.startActivity(startIntent)
}
}
}
MainActivity
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
appContext = applicationContext
ServiceModul.startServices(this)
finish()
}
companion object {
lateinit var appContext: Context
}
}
I signed the application, it has permissions, however on boot, it could not find MainActivity. I check package names like 3-4 times. What should i do?
I have BroadcastReceiver which listens for ACTION_SHUTDOWN and some other actions.
My problem is, when I shutdown my android device(2.3.6) BroadcastReceiver is catching ACTION_SHUTDOWN two times. Problem is only with ACTION_SHUTDOWN and not with other actions.
When I run same code on emulator, it works fine.
Guys please help me. Here is my code:
my BootReceiver.java
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_SHUTDOWN.equalsIgnoreCase(intent.getAction())) {
Log.i("Boot Receiver - Shutdown event");
// database operation
}
if(Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) {
// some operation
}
if(Intent.ACTION_AIRPLANE_MODE_CHANGED
.equalsIgnoreCase(intent.getAction().intern())) {
// some operation
}
if(ConnectivityManager.CONNECTIVITY_ACTION
.equalsIgnoreCase(intent.getAction())) {
// some operation
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz.ui"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/spota"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name="com.xyz.UserActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.xyz.BootReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.intent.action.BATTERY_CHANGED" />
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
</application>
And here is the Logcat entries
05-03 16:37:23.826: I/xyz(2337): Boot Receiver - Shutdown event
05-03 16:37:23.881: I/xyz(2337): Inserting Data
05-03 16:37:28.514: I/xyz(2337): Boot Receiver - Shutdown event
05-03 16:37:28.529: I/xyz(2337): Inserting Data
Thanks!
you can add a flag to avoid this.
once you received this intent, set the flag.
if(flag)
{
do sth.
flag =0 ;
}
else
{
ignore.
}
I really do not know what is wrong, but my if widget is update by system by "APPWIDGET_UPDATE" it throws following exception. I tried several things, exporting receiver (true/false), I tried it on emulators and real phones, but it is same. I added several intent-filters, but it did not work.
11-06 20:10:10.279: W/ActivityManager(61): Permission denied: checkComponentPermission() reqUid=1000
11-06 20:10:10.279: W/ActivityManager(61): Permission Denial: broadcasting Intent { act=android.appwidget.action.APPWIDGET_UPDATE (has extras) } from com.ency.easychange (pid=1196, uid=10034) requires null due to receiver com.android.settings/com.android.settings.widget.SettingsAppWidgetProvider
My AppWidgetProvider is only declared, because I tried to eliminate possibilities, but the exception is throw before ExchangeRateWidgetProvider.onReceive() is called.
public class ExchangeRateWidgetProvider extends AppWidgetProvider {
public static final String tag = "ExchangeRateWidgetProvider";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
}
}
My Manifest:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- TODO: Remove, only for traces -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="#drawable/ic_launcher_main"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".EasyChange"
android:label="#string/title_activity_easy_change" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExchangeRateWidgetService"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
</intent-filter>
</service>
<receiver
android:name=".ExchangeRateWidgetProvider"
android:label="#string/exchange_rate_widget_name"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/exchange_rate_widget_providerinfo" />
</receiver>
<activity
android:name=".WidgetConfigSmall"
android:label="#string/title_activity_widget_config_small"
android:theme="#android:style/Theme.Holo.Dialog"
android:excludeFromRecents="true"
android:exported="true" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
</application>
I appreciate if you can take a look ...
Your problem lies somewhere in your Java code, where you are attempting to send the android.appwidget.action.APPWIDGET_UPDATE broadcast. That is to be broadcast by the OS, not by apps, and that is what the Permission Denial is about.
Manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AlarmActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name="CallReciver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE">
</action>
</intent-filter>
</receiver>
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:enabled="true"
android:name=".AlarmService">
</service>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
<uses-permission android:name="android.permission.WRITE_SMS">
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS">
</uses-permission>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
Receiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class OnBootReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("Test","booot");
Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
}
}
Receiver doesn't work. I turn off and on my device and nothing happens.
SMS And Call Receiver in this project work good.
SMS Receiver and CallReceviver - works good.
First post updated - added full manifest.
If you have HTC device you also need to register for "android.intent.action.QUICKBOOT_POWERON". So the entry in manifest should be:
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
On my HTC, if I turn off the device and turn it on for a while I got QUICKBOOT_POWERON and no BOOT_COMPLETED.
If I turn off the device and remove the battery for a while - I got BOOT_COMPLETED after start.
Put permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
Also know that in Android >= 3.1 the app gets installed in 'stopped' state and will not get boot and shutdown events until the user 'does something' with the app at least once. See this post on the topic.
Try this::
<receiver android:enabled="true" android:exported="true"
android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Cheers...!!!