my sms receiver can't accept new sms - android

i want to receive sms on my application, but when i try to get new sms my application didn't get the new sms. I cannot find where I am doing wrong. I'm not sure if there's something wrong with the code, or debugging.
I'm trying to be notified if a new SMS arrives and save the sms on my database.
this is my receiver.
public void onReceive( Context c, Intent i) {
Bundle b = i.getExtras();
SmsMessage[] m = null;
String s = "";
TelephonyManager teleponyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
int x;
if (b != null) { //IF valid SMS
Object[] p = (Object[]) b.get("p");
m = new SmsMessage[p.length];
for (x = 0; x < m.length; x++) { //FOR ambil konten SMS
m[x] = SmsMessage.createFromPdu((byte[])p[x]);
s += "SMS dari " +m[x].getOriginatingAddress().toString().trim();
s += " :";
s += m[x].getMessageBody().toString();
s += "\n";
}
Toast.makeText(c, s, Toast.LENGTH_LONG).show();
nomor = m[x].getOriginatingAddress().toString().trim();
pesan = m[x].getMessageBody().toString();
Cursor cursorKontak = data.pilihKontak(nomor);
if(cursorKontak.moveToFirst()) {
idkontak = cursorKontak.getString(cursorKontak.getColumnIndex("idkontak"));
}
if(idkontak == null) {
nama = nomor;
data.inputKontak(nama, nomor);
Cursor cursorKontak2 = data.pilihKontak(nomor);
if(cursorKontak2.moveToFirst()) {
idkontak = cursorKontak2.getString(cursorKontak2.getColumnIndex("idkontak"));
}
data.inputPesanMasuk(idkontak, pesan);
}else {
data.inputPesanMasuk(idkontak, pesan);
}
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", s);
c.sendBroadcast(broadcastIntent);
my android manifest
Update
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.sms" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<activity android:label="#string/app_name"
android:name="EnkripsiSMS" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".SMSReceiver" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<activity android:name=".KotakMasuk"/>
<activity android:name=".TulisPesan"/>
<activity android:name=".KotakKeluar"/>
<activity android:name=".HasilEnkripsi"/>
<activity android:name=".KirimPesan"/>
<activity android:name=".IsiPesanKeluar"/>
<activity android:name=".DekripsiPesanKeluar"/>
<activity android:name=".HasilDekripsiPesanKeluar"/>
<activity android:name=".TeruskanPesanKeluar"/>
<activity android:name=".KirimPesanKeluar"/>
<activity android:name=".IsiPesanMasuk"/>
<activity android:name=".DekripsiPesanMasuk"/>
<activity android:name=".TeruskanPesanMasuk"/>
<activity android:name=".HasilDekripsiPesanMasuk"/>
<activity android:name=".Balas"/>
<activity android:name=".HasilBalas"/>
<activity android:name=".KirimPesanMasuk"/>
</application>
</manifest>
can somebody help me?
i really need the solution.
thanks..

Instead of p use pdus
Object[] p = (Object[]) b.get("pdus");

Related

Didn't find class on path: DexPathList

I used a receiver in this code after running and receiving a sms this error is shown. I try some thing. I create new project and add file again. delete bin file. clean and restart project. but It doesn't solve the error. Is there anything that can solve this error. I really cant solved it.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="blocker.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".FirstPage"
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=".receiver.SMSReceiver" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".Search"
android:label="#string/app_name" >
</activity>
<activity
android:name=".BlockActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SmsFilter"
android:label="#string/app_name" >
</activity>
<activity
android:name=".CustomAdapter"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
error:
property:
the class for sms receiver:
public class SmsFilter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[])extras.get("pdus");
if (pdus.length < 1) return; // Invalid SMS. Not sure that it's possible.
StringBuilder sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (sender == null) sender = message.getOriginatingAddress();
String text = message.getMessageBody();
if (text != null) sb.append(text);
}
Log.i("LOG: ", "sms recived");
Log.i("LOG: ", sender);
if (sender != null && Search.search(sender)==true) {
abortBroadcast();
}
return;
}
}
// ...
}
}
Please check again the name of the receiver and the class name you declared in your manifest receiver.
In in java file the class name is-SmsFilter extends BroadcastReceiver
SmsFilter extends BroadcastReceiver
As you can see the name is SmsFilter but in your manifest file you are declaring it as SMSReceiver
The names are not matching. Edit your manifest to something like this-
<receiver android:name=".receiver.SmsFilter" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
I hope it would help you..

can not receive a message (broadcast recceiver ) through this code

am not getting a message through my app - help me out i dont know what changes i have to make in this code , tell me should i make it to default(so it will give me a message) or something else do in my code - i do receive a message but not from this app
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage
.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum
+ "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "
+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.smsmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<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>
</application>
I think, you should add this code
<receiver android:name=".yourService" android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
in application tag in Manifest
or
or you must register your InComingservice in activity class.
For example
private InComingSMSReceiver inComingSMSReceiver = new InComingSMSReceiver();
#Override
protected void onResume() {
super.onResume();
registerReceiver(inComingSMSReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(inComingSMSReceiver);
}
It is correct answer , MainAtivity.java class was fine but i did make changes in manifest as u people helped me. and its working perfectly
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.smsmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".IncomingSms"
android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</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>

Android SMS dynamic Broadcast

I have a Program that is sending a SMS to a device witch answers with an SMS.
I want to receive the SMS in an give the message back to my program.
Cause I will only wait for a certain time I need a dynamic registration
Here my Code for the Breadcast Receiver
public class SMSReceiver extends BroadcastReceiver {
private MainActivity father = null;
private String call_number = null;
public void setFather (MainActivity father) {
this.father = father;
call_number = father.getCallNumber().trim();
while (call_number.startsWith("0")) {
call_number = call_number.substring(1);
}
}
public void onReceive(Context cxt, Intent intent)
{
//if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
String sender = smsMessage[0].getDisplayOriginatingAddress();
String mess = smsMessage[0].getDisplayMessageBody();
String toast = "Received SMS from: " + sender + "\nMessage: " + mess;
//if ((sender != null) && (sender.endsWith(call_number))) {
father.setSMSMessage((mess ==null ? "": mess)+ "\n from "+sender);
//}
Toast.makeText(cxt, toast, Toast.LENGTH_LONG).show();
}
}
}
Here my Code for the Registration
myreceiver=new SMSReceiver();
myreceiver.setFather(this);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(myreceiver, filter);
Here my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.standheizung"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<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=".SMSChecker">
<intent-filter>
<action android:name="SMS_SENT" />
<action android:name="SMS_DELIVERED" />
</intent-filter>
</receiver>
</application>
</manifest>
My problem is that I get a SMS from the Device but my receiver does nothing.
Comment the Receiver for Sending the SMS works without any Problem
Has anybody an idea whats the problem ???

Recieving text messages within my app

The aim of my app is to grab in a text message and then display it on screen within a dialog box. It should also make it so that the message does not appear in the phones inbox. Currently i've coded what i think should work but when i send texts to the phone, they just appear in the inbox with no response from the app at all.
code below:
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message",str);
context.startActivity(act);
//abortBroadcast();
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cam.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECIEVE_SMS"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.cam.sms.MainActivity"
android:label="#string/app_name"
android:alwaysRetainTaskState="True"
android:launchMode="singleInstance">"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsScreen"
android:alwaysRetainTaskState="True"
android:launchMode="singleInstance">
</activity>
<receiver android:name=".SMSReceiver" android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
add
<intent-filter android:priority="100">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
to your manifest
In Manifest add one row inside of Intent filter of your app:
<intent-filter android:priority="100">
It means that your app will first decide what to do with your message.
Also, add in your code
this.abortBroadcast();
to stop further forwarding.
EDIT: Try also adding
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Log.d("SMSReceiver",str);
BroadcastReceiver is a service. which means that it always look for the service and it is independent to the app activity.
so it won't show the application dependent Toast in service but you can call Activity using this service.
and for to receive first into your application give priority level 100 in your manifest file.
use
abortBroadcast();
method in your code

My updated SMS Receiver doesn't work Android

hi :) i'm working with SMS in android. i'm having problem with my sms receiver class. when i run my app for the first time on emulator, it works as i've programmed it to work. but when every time i run the app again, it doesn't work as i updated it to work. i'm stuck for last 2 days. can somebody plz guide me or provide some help. my basic receiver class is:
public class SMSreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String Sender = null;
String str = "";
SmsMessage [] msgs = null;
if(bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0;i<msgs.length;i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
Sender = msgs[i].getOriginatingAddress();
str = "SMS From: " + msgs[i].getOriginatingAddress();
str += ":";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//Toast.makeText(context, str, Toast.LENGTH_LONG).show();
Toast.makeText(context, Sender, Toast.LENGTH_LONG).show();
}
}
in above code, i've commented the toast that shows the msg, and tried to display the toast that shows the sender's number. but still it shows the new msg text. that's weird.
here's my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pingpongsmsremote"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="12" />
<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.pingpongsmsremote.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>
<activity
android:name="com.example.pingpongsmsremote.SMSScheduler"
android:label="#string/title_activity_smsscheduler" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.FilterSMS"
android:label="#string/title_activity_filter_sms" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.SMSRemote"
android:label="#string/title_activity_smsremote" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.SendSms"
android:label="#string/title_activity_send_sms" >
</activity>
</application>
</manifest>
It looks like maybe you forgot to register the receiver? You need a line like this in your manifest:
<receiver android:name="SMSreceiver" >
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
To see it in a full manifest example would look like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pingpongsmsremote"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="12" />
<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.pingpongsmsremote.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>
<activity
android:name="com.example.pingpongsmsremote.SMSScheduler"
android:label="#string/title_activity_smsscheduler" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.FilterSMS"
android:label="#string/title_activity_filter_sms" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.SMSRemote"
android:label="#string/title_activity_smsremote" >
</activity>
<activity
android:name="com.example.pingpongsmsremote.SendSms"
android:label="#string/title_activity_send_sms" >
</activity>
<receiver android:name="SMSreceiver" >
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>

Categories

Resources