BroadcastReceivers on android 4.0.3 (ICS) - android

I have been trying to use the BroadcastReceiver on ICS from a while now.
I have tried all kinds of solutions, but nothing worked for me.
Here is how my manifest file looks like...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sp.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".XYZActivity"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ScreenOffReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
</receiver>
</application>
</manifest>
And here is my Receiver code...
package com.sp.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ScreenOffReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.e("AppSearch", "Message");
}
}
The activity class has some code in it. But now this broadcast receiver never gets registered it seems. The onReceive() method does not get called. I could not find any solution. All the threads mentioned that I need to have at least one activity, but I already have it.
Thank you in advance!

You also need to start the activity (via the launcher) at least once, otherwise your app will be in a 'stopped' stated, and won't receive any broadcasts.

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.

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?

How to properly auto-start APP

i'm having trouble with the auto-start feature of my APP.
I've searched trhough the forums, seen many suggestions, none seems to work and I don't know why.
Here's the BootUpReciver.java
package com.???.???;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class BootUpReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
//Intent pushIntent = new Intent(context, BackgroundService.class);
//context.startService(pushIntent);
Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show();
Log.d("TAG","Device Booted");
}
}
}
And here's my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.???.???"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:theme="#android:style/Theme.Holo"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".BootUpReciver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="com.???.???.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>
</application>
When i restart my phone i don't even see the Toast, why? what should i do? I need to auto-start to get some sharedprefs and write a value to a file every boot.
Thanks.
While testing I found that If I show toast in onReceive then it is not showing. But if I start an activity in onReceive then it is working. So you can also check this. Start an activity or service and perform your task there.

Android BroadcastReceiver not running when designated

I am new to android development, and
I have an app that when run from the app icon in the menu / home screens runs just fine. However when I try to make it run on boot completion it crashes (Keeping in mind that if I go back to the Icon it will still work) I am wondering what is wrong here. Keep in mind I am doing testing on my personal phone which is android 2.3 and it is ROOTED. Some how I don't think this should be as hard as it is to do, since 9/10 most android apps work in this way or in a way similar to this. Simply put: What am I doing wrong?
In the mean time here is my code for the Broadcast Receiver:
package path.to.file;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class autoBot extends BroadcastReceiver {
private static final String LOG_TAG = "StartAtBootServiceReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("path.to.file.MainActivity");
context.startActivity(i);
}
}
}
As well as the Manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
installlocation="internalOnly"
package="path.to.file"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="autoBot" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
<category android:name="android.intent.category.HOME" >
</category>
</intent-filter>
</receiver>
</application>
</manifest>
Is there a specific reason you are adding an action to your intent? If your only aim is to launch your activity at start-up, do this in your BroadcastReceiver:
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Also, remove these lines from your manifest under receiver tag, you won't need them there:
<category android:name="android.intent.category.HOME" ></category>

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