Android broadcast is not working when the app is not running - android

Please don`t vote down if this is a silly question please share some light here
i want to created an app for saving call history
for that i followed
http://karanbalkar.com/2014/02/detect-incoming-call-and-call-hangup-event-in-android/
this site
but i`m not getting the event when the app is not running.Please help
i want to get the event when a incoming call is received even when the app is closed
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sars.broadcasttest">
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".IncommingCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
class file
package com.sars.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* Created by athulbabu on 18/08/15.
*/
public class IncommingCall extends BroadcastReceiver {
int i=0;
#Override
public void onReceive(Context context, Intent intent) {
Log.d("IncomingCall", "Call catched test"+i++);
Toast.makeText(context, "Calll Intent Detected. test", Toast.LENGTH_LONG).show();
}
}

For security reasons you have to run app at least once after install to get the BroadcastReceiver working.
Read this section: Launch controls on stopped applications
http://developer.android.com/about/versions/android-3.1.html

Related

How to know why receiver is not calling class and onReceive not executing

Im building an android app to detect incoming calls. It use to work a year ago, but something i´ve change has mess it up.
I reduce the code to the basis to debug, but i cant still make a Toast Notification apear when phone is reciving calls.
Has it been some major change to the permissions with reading phone state? I´ve made the modification for androdi 9 adding the ReadCallLog permision, but nothing seems to change...
AndroidManifest.xml
<?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.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<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="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
MyPhoneReceiver.java
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Eureka!", Toast.LENGTH_SHORT).show();
}
}
See that and this example. You need to approve the runtime permission READ_PHONE_STATE.

BroadcastReceiver app shows bugs

I made this really small app that will just listen to any ringer mode change broadcasts, and trigger a toast to show on the screen saying "Ringer Mode Changed". I don't know where I am going wrong. The IDE is now showing any errors, so maybe there is a semantics error or such.
Here is the Receiver.java file:
package com.example.android.testingbroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Ringer Mode Changed", Toast.LENGTH_SHORT).show();
}
}
Here is the AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.testingbroadcastreceiver">
<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=".Receiver" android:exported="true">
<intent_filter>
<action android:name="android.media.RINGER_MODE_CHANGED"/>
</intent_filter>
</receiver>
</application>
</manifest>
The MainActivity.java file does not have anything related to this.
I am not entirely sure what you're exact problem is, but if Receiver.java is its own class and not a fragment, try replacing the "Context" in your toast with "this".
Your code should look like this:
Toast.makeText(this, "Ringer Mode Changed", Toast.LENGTH_SHORT).show();

How to Launch an App After Rebooting / Startup

I've read some of the threads here explaining about launching an app after startup such as this one but it didn't work for me.
Here is MainActivity.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
AfterBootReceiver.java:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AfterBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("Intent.ACTION_BOOT_COMPLETED")) {
Toast.makeText(context, "AfterBootReceiver - boot", Toast.LENGTH_SHORT).show();
}
}
}
Here is the manifest with the permission code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.liorle.startappafterboot">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="false"
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=".AfterBootReceiver">
<intent-filter>
<action android:name= "android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
My questions is: does any of you see any problem with this code? how to make the receiver receive the boot action from within the main activity?
Thanks!
You will need to register the receiver outside of your main Activity (in your Manifest), Android will not know that your BroadcastReceiver exists until your Activity is started. You will also need to create your own BroadcastReceiver implementation for this purpose.
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
You may start yourt main Activity from inside your newly created BroadcastReceiver (BootCompletedReceiver).
Your application will also need to request the RECEIVE_BOOT_COMPLETED permission.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Your launcher is not listening for the BOOT_COMPLETED broadcast.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
In your apps current state - the main activity needs to be running to listen to the BOOT_COMPLETED broadcast. Your manifest needs to have something registered that tells the ecosystem to activate the activity / receiver.

android BroadcastReceiver doesn't get called

I've seen a lot of question like this around and I've tried a bunch of things without success...
I just want to log a sms received using a subclass of BroadcastReceiver like I've seen in so many tutorial...
so here is my code and config
BroadcastReceiver
package com.example.koki.mysms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("MainActivity", "test");
Toast.makeText(context, "ON RECEIVE BROADCAST", Toast.LENGTH_LONG).show();
}
}
there's nothing in my MainActivity beside what you get when creating a new projet with android studio.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.koki.mysms" >
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<application
android:allowBackup="true"
android:icon="#mipmap/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=".SmsReceiver" android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
the way I test it (not very familiar in android development yet), is that I have two emulator and I sent sms to one another. They both receive the sms but nothing in the logcat nor the Toast appear. what am I doing wrong?

Android App Not Responding the SMS Receiver

I have been working on an app to respond to received SMS messages (before you complain, I know this has been asked A LOT, but believe me, I can't get it to work at all, and I've tried searching for hours). I've got my manifest set up like so:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.csbctech.notiscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".NotiScreenActivity"
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="NotiScreenSmsReceiver" android:process=":remote">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
And my receiver class looks like this:
package com.csbctech.notiscreen;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.PowerManager;
import android.util.Log;
public class NotiScreenSmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.w("NotiScreen", "Got SMS");
}
}
But the "NotiScreenSmsReceiver" class never gets called. I've tried several different examples, and I can't for the life of me get the receiver class to get called...I've even tried removing the uses-permission, and I don't even get an error message about not having permissions. What could be wrong? Oh please help me, you're my only hope!
Are you running this on a phone with a custom SMS app such as GOSMS? I've heard that some of those programs will stop the broadcast so that they can create their own notifications and stop the stock ones.
Change
<receiver android:name="NotiScreenSmsReceiver" android:process=":remote">
to
<receiver android:name=".NotiScreenSmsReceiver" android:process=":remote">

Categories

Resources