Not able to start Service using Broadcast Receivers - android

I want to start a service using BroadcastReceiver with this code
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context,BlueToothService.class);
context.startService(myIntent);
}
But not able to start the service.
I also registered service and receiver in manifest.
And I have also one doubt, can we use Broadcast Receiver without activity?
This is my service class
public class BlueToothService extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onStartCommand(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "service Started", Toast.LENGTH_LONG);
doBluetoothJob();
}
My manifest file looks like this.
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BROADCAST_SMS" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
</application>
<service
android:name=".BlueToothService"
android:enabled="true" >
</service>
<receiver android:name="com.simsys.bt.DemoBT" >
</receiver>
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

This is working with me i created a Receiver.
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "MyReceiver Started", Toast.LENGTH_SHORT).show();
Intent myIntent=new Intent(context,MyService.class);
context.startService(myIntent);
}
}
then create a Simple Service
public class MyService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId){
Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT).show();
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
}
don't forget to make Entry for Broadcast Receiver and service in manifest file
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service
android:enabled="true"
android:name=".MyService">
<intent-filter>
<action
android:name = "com.rdc.MyService">
</action>
</intent-filter>
</service>
<receiver
android:enabled="true"
android:name=".MyReceiver">
<intent-filter>
<action android:name = "android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
now after reboot the emulator Toast will appear.

The manifest file you posted is incorrectly formulated. The <service> and <receiver> tags need to be inside the <application> tag. Like this:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<service
android:name=".BlueToothService"
android:enabled="true" >
</service>
<receiver android:name="com.simsys.bt.DemoBT" >
</receiver>
</application>

Try showing your toast with:
Toast.makeText(this, "service Started", Toast.LENGTH_LONG).show();
And you need to give the service declaration in the manifest an specific intentfilter with the intent you want it to listen to like:
<receiver android:name="com.simsys.bt.DemoBT" >
<intent-filter>
<action android:name="your.intent.action.definition" />
</intent-filter>
</receiver>

You need to override onStartCommand() not onStart() in your Service subclass. onStart() is depreciated, it should still be called but it would be recommended to override onStartCommand() and see if that works.
Additionally, in your manifest file the service, receiver and intent-filter tags should be children of the application tag. The receiver also needs to declare what intents it will process. e.g.
<receiver android:name="com.simsys.bt.DemoBT" >
<intent-filter>
<action android:name="com.simsys.bt.intents.StartService" />
</intent-filter>
</receiver>

Related

Why Service in not running in Background after remove for recent apps

Why my Service is not running in background when if remove my app from background or remove from recent apps.
I very much confuse between IntentService and Service which one I should prefer.
I am making app where I will read all call log detail and will send automatic SMS therefore I need to run service background.
But the problem is when I'm running this app is working fine also when I'm minimizing it then also working fine but the moment when i closed or remove the apps from background it no longer working.
This is my Manifest file
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mytraining.com.callrecord">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service
android:name=".PhoneReceiver"
android:enabled="true"
android:exported="true"></service>
<receiver android:name=".PhoneCallReceiver">
<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>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Is there any change need to do or not.
And also it's also not working in Above oreo
PhoneReceiver.java
public class PhoneReceiver extends Service {
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber;
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service is Running", Toast.LENGTH_SHORT).show();
}
#Override
public void onDestroy() {
super.onDestroy();
Intent restartIntent = new Intent("Restart");
sendBroadcast(restartIntent);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Service is Oncommand", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
Try using return start sticky but there is a catch to this some manufacturer like Vivo,Oppo,Mi will still kill your service and starting from Oreo you need to show notification to keep your service running else android will kill it .
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Service is Oncommand",
Toast.LENGTH_SHORT).show();
return START_STICKY;
For more info refer this link.
https://developer.android.com/reference/android/app/Service#START_NOT_STICKY

android:register receiver after restart

i have created a BroadcastReceiver called Autostart to be called, when the device is booted:
public class Autostart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1)
{
Log.i("mydebug","autostart");
Intent intent = new Intent(context, RegisterService.class);
context.startService(intent);
}
}
this is the Service that is supposed to be called:
public class RegisterService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("mydebug","service");
BroadcastReceiver receiver = new NetWatcher();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(receiver, intentFilter);
return super.onStartCommand(intent, flags, startId);
}
}
and here my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ginso.podcasts">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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=".NetWatcher">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".RegisterService"/>
</application>
</manifest>
i also register the NetWatcher in my MainActivity the same way. Now when i start the app, the Netwatcher immediatly gets called and also everytime i change my wifi state. When i restart my phone, the autostart calls the service, which registers the NetWatcher, which also gets immediatly called. But if i change the wifi state now, nothing happens
register NetWatcher in onCreate() of your service
Try Changing your action to android.net.conn.CONNECTIVITY_CHANGE instead WifiManager.NETWORK_STATE_CHANGED_ACTION

BootReceiver class is not working and closes on manual restart

I would like to build a boot receiver.
Manifest.XML
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
And this is my Receiver Class:
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e("-->", "BOOT COMPLETED");
}
}
If i restart my device i get the message (on my device):
the app xxx was closed
Try to change this in your .manifest
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Also Try this out in your Reciever
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "Receiver Called", Toast.LENGTH_LONG).show();
}
}
Also Add android:persistent="true" in to your manifest tag

Service not starting at phone reboot - Android

I have been trying to workout this problem for the past couple of hours looking at various code samples here but still haven't found a solution.
I want a service to start when app starts and when the phone is reboot the service should start again automatically without starting the app. (Neither the toast or log appears when i restart my phone). Please see my code below:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
MyService.java
public class MyService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startid){
Toast.makeText(this, "Service Running ...", Toast.LENGTH_LONG).show();
Log.v("Service","Service Running ...");
return super.onStartCommand(intent, flags, startid);
}
}
Autostart.java
public class Autostart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MyService.class);
context.startService(myIntent);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxxx.receiverservice" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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:name=".Autostart"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyService" />
</application>
</manifest>
Seems like this is a bug on Xiaomi. I haven't encountered this problem with any other device

Why isn't my Android boot receiver working?

I am developing a small android application in which I wanted to start some service once the device boot is completed.
This is the code I used to try to accomplish this, but it not receiving boot complete event, so it isn't working.
//in manifest file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
//my schedule receiver
public class MyScheduleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
{
Log.i("*************************TEST", "Service loaded at start");
Toast.makeText(context, "device started", Toast.LENGTH_SHORT).show();
}
}
}
MyScheduleReceiver never gets triggered; am I doing something wrong?
You miss a dot before MyScheduleReceiver
<receiver android:name=".MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Categories

Resources