I tried out many ways but my receiver is not working. I am stuck in this from long time and have tried everything possible or I can understand but didn't found solution of this problem. Please find the code below.
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cyberdashtechnologies.scanhubnt">
<uses-permission android:name="android.permission.INTERNET" />
<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/Theme.ScanhubNt"
android:usesCleartextTraffic="true">
<activity android:name=".profile"
android:screenOrientation="portrait"
/>
<activity android:name=".support"
android:screenOrientation="portrait"/>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"
android:screenOrientation="portrait"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MyReceiver
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
if(Objects.equals(intent.getAction(), Intent.ACTION_BATTERY_CHANGED) || Objects.equals(intent.getAction(), Intent.ACTION_BOOT_COMPLETED)){
context.startForegroundService(new Intent(context,location.class));
}
}
Can you please tell where I am doing wrong? so that issue can be resolved