I have a BroadcastReceiver declared in the following AndroidManifest.xml, but I never get the SMS_RECEIVED broadcast. Any ideas what is happening?
This is the BroadcastReceiver:
public class SMSReceiver1 extends BroadcastReceiver
{
private static final String LOG_TAG = "Thiri The Wut Yee";
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent)
{
Log.i(LOG_TAG, "Receive ");
Toast.makeText(context, "RECEIVED", Toast.LENGTH_LONG).show();
}
}
And the AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android_programmers_guide.SMSReceiver1"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".SMSReceiver1"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
Do you have this permission in your manifest?
<uses-permission android:name="android.permission.RECEIVE_SMS" />
Related
I've a broadcast receiver and it is not getting hot even though the broadcasts re registered in the manifest.
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trial.trialservice"
android:versionCode="2"
android:versionName="BA_1.00.0.0.001"
android:sharedUserId="android.uid.system" >
<uses-sdk
android:minSdkVersion="21"
/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="false"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Material" >
<service
android:name="com.trial.trialservice.TrialService"
android:exported="true"
android:permission="com.encoding.permission.TRIAL_ACCESS" >
</service>
<receiver android:name=".TrialHandler" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.trialmanager.TRIAL_STATUS_UPDATED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is the handling part of the code:
public class TrialHandler extends BroadcastReceiver {
private final String TAG = "TrialHandler";
private Context mContext;
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "Inside TrialHandler onReceive, Intent: "+intent);
mContext = context;
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) ||
intent.getAction().equals("com.trialmanager.TRIAL_STATUS_UPDATED")){
// My business logic goes here
}
}
}
Any help is greatly appreciated. Thanks.
I followed a tutorial on Receiving sms using BroadcastReceiver
https://www.youtube.com/watch?v=h-zYXVODiPo
I used exactly the same codes. Enabled Telnet client. Does my device need to be rooted first? I don't know the problem. Please help.
Here's my MainActivity
public class MainActivity extends Activity {
BroadcastReceiver receiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arr0, Intent arr1) {
processReceiver(arr0,arr1);
}
};
registerReceiver(receiver,filter);
}
public void onDestroy(){
super.onDestroy();
unregisterReceiver(receiver);
}
public void processReceiver(Context context,Intent intent){
Toast.makeText(context,"RECEIVED",Toast.LENGTH_LONG).show();
TextView lbs = (TextView)findViewById(R.id.textView1);
Bundle bundle = intent.getExtras();
Object[] objArr = (Object[])bundle.get("pdus");
String sms="";
for(int i=0;i>objArr.length;i++){
SmsMessage smsMsg = SmsMessage.createFromPdu((byte[])objArr[i]);
String smsBody = smsMsg.getMessageBody();
String senderNumber = smsMsg.getDisplayOriginatingAddress();
sms+= "From: "+senderNumber+"\nContent: "+smsBody+"\n";
}
lbs.setText(sms);
}
Here's my Manifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gavadev.com.smsreceiver">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"></activity>
</application>
</manifest>
Use a Broadcast Receiver which will not be unregistered when destroying the activity:
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
// your processing will go here...
}
}
}
Register the receiver via manifest file as following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiverpremier"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.broadcastreceiverpremier.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="MyReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
I am trying to do some work at incoming call times. I am trying like this
public class callDeductor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();
}
}
}
This is my Manifeast:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deduct.calldeduct"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="Call Deduct" >
<receiver android:name="com.deduct.calldeduct.callDeductor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
I check with mobile and emulator. But it does not show any toast message. Please let me know where I did mistake.
I got reference from this websites.
Link 1
Link 2
Continuing my conversation with asker as an answer.I Just started a new Android application and worked out like i have said in the comments.. The manifest looks like this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dj.randomtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".CallDetector">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<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>
</application>
The CallDetector class looks like this(ur code):
public class CallDetector extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();
}
}
}
Refactored my simple dial application and moved my custom 'ServiceReceiver' class so it is nested in my Main Class like so (partial code showing just nested ServiceReceiver class)
public class Main extends Activity implements OnClickListener{
public class ServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Log.d("foo","incomingNumber : "+incomingNumber);
EditText incomingNum = (EditText) findViewById(R.id.inputText);
incomingNum.setText(incomingNumber);
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
My question is, after doing this, what changes need to be made to the manifest.xml?
Here is my existing (old) manifest file when ServiceReceiver was in a seprate file, class, not nested.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.foo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.foo.Main"
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=".ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
Did you try something like this:
<receiver android:name="com.example.foo.Main$ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
and make the Receiver class as static
This is a simple program i created to test BOOT_COMPLETED event of Android but it's not working! am i doing something wrong here?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.enea.training.bootdemo"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".BootDemoReceiver">
<Intent-filter>
<action android:name ="android.intent.action.BOOT_COMPLETED"></action>
</Intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
java:
public class BootDemoReceiver extends BroadcastReceiver {
static final String TAG = "BootDemoReceiver";
#Override
public void onReceive(final Context context, final Intent bootintent) {
Log.v(TAG, "Come on");
}
}
I'm not sure if it matters, but try changing <Intent-filter> to <intent-filter> (note lowercase 'i').