I have the latest update for my Samsung Galaxy S2 and now my application stop working (App don't receive sms). I have SMSReceiver extends from BroadcastReceiver with SMS_RECEIVED action.
It looks like this:
private class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context cntxt, Intent intent) {
Log.i(TAG, "New broadcast receiver");
SharedPreferences settings = getSharedPreferences(TAG, 0);
if (settings.getString("authCode", "").equals("")) {
Log.i(TAG, "Get sms");
if (intent.getAction().toString().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
if (messages.length > -1) {
Log.i(TAG, "Read sms");
}
}
}
}
}
I register BroadcastReceiver like this:
mSmsReceiver = new SMSReceiver();
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
filter.setPriority(100000);
registerReceiver(mSmsReceiver, filter);
Everything works on HTC Desire or Desire Z and android emulator. On Galaxy S2 my SMSReceiver don't receive any Intent when SMS came. I tried all solution but it didn't have any effect.
Probably a issue with the latest version of GoSMS
Had the same issue with the "android.net.conn.CONNECTIVITY_CHANGE" action on the Galaxy s2. Couldn't receive reconnection intent when the Broadcast receiver was registered in the code.
Registering the broadcast receiver in the manifest was the solution for me.
Related
In BroadcastReceiver i want to receive sms from default Mobile application.
But when user send sms without internet connect then i receive, but when user (send sms via wifi or Mobile Network) i unable to read message.
public class MessageReciver extends BroadcastReceiver {
private static MessageListener mListener;
public static final String reciveSMS="android.provider.Telephony.SMS_RECEIVED";
public static final String TAG ="SmsBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i("fsdfdsfdsfdgfdsg "," rerwerw");
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle data = intent.getExtras();
Object[] pdus = (Object[]) data.get("pdus");
String formate = data.getString("format");
for (int i = 0; i < pdus.length; i++) {
SmsMessage smsMessage = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i], formate);
} else {
smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
String message = smsMessage.getMessageBody();
Toast.makeText(context, "Message Received: " + message, Toast.LENGTH_SHORT).show();
}
}
}
}
I have't understand why i face this problem , because Broadcast receiver can send or receive broadcast messages from the Android system and other Android apps. It only receive (sms without using internet), But every mobile messages application have default enable option(Use wifi or data for messaging when available).
Anyone please help receive sms from default Mobile Messages Application
already register your receiver yet?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(messageReciver , new IntentFilter(YOUR FILTER));
}
try this
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application>
// add this
<receiver android:name=".MessageReciver">
<intent-filter android:priority="1">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( ContextCompat.checkSelfPermission(
this,
Manifest.permission.RECEIVE_SMS
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.RECEIVE_SMS}, 1);
}
}
It's true, since some month before I have the same problem. If wifi is off and LTE is off, then MMS and SMS actions are broadcasted with sms_received or wap_push_received are recognized by broadcast receiver intent.
In new Smartphones these messages are marked by green color.
But if the sender phone turn on wifi or turn on LTE data mode, then shit happens.
These sms or mms over wifi or over LTE are now CHAT messages. These chat messages are not broadcasted by sms_received or wap_push_received. These CHAT messages are blue marked on modern phones. If you try to send messages (sms or mms) to an smartphone with wifi off or LTE off, you will be informed that now your messages will be send only as normal SMS or MMS.
You can find these CHAT messages over contentresolver content://im/chat
Unfortunately I doesn't know yet, how to receive broadcasted chat messages. For now I pull every 1 second the contentresolver for new chat messages.
I am trying to discover all available bluetooth devices and pass to another activity.
However even when looking at the Android Docs, I am unable to figure out why I cannot discover any devices and my ArrayList remains empty.
OnClick execute this:
mBluetoothAdapter.startDiscovery();
My broadcast listener also works but nothing is every returned and ArrayList remains empty.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(device);
showToast("Found device " + device.getName());
}
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
if (state == BluetoothAdapter.STATE_ON) {
showToast("Enabled");
showEnabled();
}
}
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
mDeviceList = new ArrayList<>();
mProgressDlg.show();
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
mProgressDlg.dismiss();
Intent newIntent = new Intent(MainScreen.this, DeviceListActivity.class);
if (mDeviceList.isEmpty()){
Log.d("onReceive: ", "EMPTY");
}
newIntent.putParcelableArrayListExtra("device.list", mDeviceList);
startActivity(newIntent);
}
}
};
GIST
Answered my own question.
The fix was to turn location on with app permissions.
Apparently this is required for Android Marshmallow
I have created a service which launch Activity after receiving SMS from certain numbers.
There are 2 Activities Success and Failure.
Both Activities opens successfully only 1 time, On second iteration, when i send SMS again, it does not effect Activity Screen.
It remains open the current Activity screen and does not change it based on the condition.
I searched this on net and found various solution, but nothing seems working here, I tried to change flags, but it only allows 1 flag from service class, and if i choose other Flags it gives me following error message.
StartActivity from outsite an activity context requires the FLAG_ACTIVITY_NEW_TASK flag
This is the service class i have written. Kindly check this and guide me what i am doing wrong here.
Thanks
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
int duration = Toast.LENGTH_LONG;
#Override
public void onReceive(Context context, Intent 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();
if(senderNum.equals("345"))
{
Intent successScreen= new Intent(context, SuccessActivity.class);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(successScreen);
}
else if(senderNum.equals("3450") || senderNum.equals("3451") || senderNum.equals("3452")){
Intent alertActivity = new Intent(context, FailureActivity.class);
alertActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alertActivity);
}
Try to replace your code with this code:
if(senderNum.equals("345"))
{
Intent successScreen= new Intent(context, SuccessActivity.class);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
successScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
context.startActivity(successScreen);
}
else if(senderNum.equals("3450") || senderNum.equals("3451") || senderNum.equals("3452")){
Intent alertActivity = new Intent(context, FailureActivity.class);
alertActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
successScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
context.startActivity(alertActivity);
}
I have a mUsbReceiver (BroadcastReceiver) and CameraActivity. The receiver setContentView(R.layout.main) from CameraActivity via an Intent. Then CamearActivity updates its View with this value. Notice that the setContentView is in the Broadcast receiver class and not in the CameraActivity Class.
public class CameraActivity extends Activity {
private static final String TAG = "openXC::Activity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
usbConnection();
}
public void usbConnection() {
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_CANCEL_CURRENT);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
String txt = "default";
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Log.i(TAG, "Device List: " + deviceList);
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
UsbDevice device = deviceIterator.next();
Log.i(TAG, "Device List: " + deviceList);
mUsbManager.requestPermission(device, mPermissionIntent);
}
private static final String ACTION_USB_PERMISSION ="com.ford.openxc.webcam.USB_PERMISSION";
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
Log.d(TAG, "Displayed Comten View " + device);
setContentView(R.layout.main);
}
}
else {
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
};
}
This works fine sometimes but sometimes throws the following error
I/openXC::Activity( 5609): Device List: {/dev/bus/usb/001/002=UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;#421a1f50]}
I/openXC::Activity( 5609): Device List: {/dev/bus/usb/001/002=UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;#421a1f50]}
I/Adreno200-EGLSUB( 5609): <ConfigWindowMatch:2087>: Format RGBA_8888.
E/ ( 5609): <s3dReadConfigFile:75>: Can't open file for reading
E/ ( 5609): <s3dReadConfigFile:75>: Can't open file for reading
D/openXC::Activity( 5609): Displayed Comten View UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;#421d3ed0]
D/WebcamPreview( 5609): WebcamPreview constructed
Technically, you can call setContentView any time you are executing on the event thread.
Otherwise you need to use a Handler to call it.
Also, here are some usefull links that might help you:
link1
link 2
link 3
I dont have much exp on USB sort of thing but since u said its saying cannot readfile.. i believe dat the error may be in the usb so for the debugging purpose i would suggest to move the setContentView(int) from if conditions to the onRecieve directly so dat whenever the onReceive is called ur contenview will change , this will help to ensure that the error is not with setcontentview... After dat u can see without setcontentview in the usb and now if the error is coming then surely the error is in the usb and not in the setContentView ....
Hope it works :)
I am creating an app that receives SMS text messages, I have created a class that extends BroadcastReceiver to receive and display SMS messagesin a toast within the app. I have also added permissions too my manifest along with the declaration of the receiver. The problem is it still does not seem to display the message. It will only appear on the top bar as a notification. Hopefully someone will be able to see where i am going wrong. Do i have to have some code in my mainactivity in order to display the message?? or should it display in my all my activities straight from the receiver clas?? code below:
UPDATE
I ended up deleting my receiver and recreating everything, now it seems to work fine
SmsReceiver.java:
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras ==null)
return;
//toast
Toast.makeText(context, "Received", Toast.LENGTH_LONG).show();
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = SMessage.getOriginatingAddress();
String body = SMessage.getMessageBody().toString();
Intent in = new Intent("SmsMessage.intent.MAIN").putExtra("get_msg", sender+":"+body);
context.sendBroadcast(in);
this.abortBroadcast();
}
}
}
The problem is it still does not seem to display the message. It will only appear on the top bar as a notification
do you know what is - Ordered Broadcast?
in case you don't know - it's a broadcast that receives synchronously (one after another) by all receivers that registered for this broadcast. you can set in the manifest receiver deceleration a priority , and each one of the receivers can control if the broadcast will pass on in the priorities chain to additional registered receivers by using the aboartBroadcast() method.
what's to this and receiving SMS? - the system sends ordered broadcast when SMS receives. if your receivers priority is lower then the "default SMS app" priority - then what usually happens is that the application that got the broadcast first will cancel the broadcast, and you'll never receives it.
to get the broadcast before the default SMS application you have to declare your receiver with high priority (providing priority = 1000 should do the trick).
without providing priority - it's automatically set to zero.
you can have a look on this example - How Can i INTERCEPT an Incoming SMS With A specific Text
also make sure you declared the permission to RECEIVE_SMS in the manifest
Try starting again and setting up your receiver like so....
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras ==null)
return;
//toast
Toast.makeText(context, "Received", Toast.LENGTH_LONG).show();
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = SMessage.getOriginatingAddress();
String body = SMessage.getMessageBody().toString();
Intent in = new Intent("SmsMessage.intent.MAIN").putExtra("get_msg", sender+":"+body);
context.sendBroadcast(in);
this.abortBroadcast();
}
}
}
#Override
public void onReceive(Context context, Intent arg1) {
SmsMessage msgs[] = getMessagesFromIntent(arg1);
for (int i = 0; i < msgs.length; i++) {
SmsMessage mesg = msgs[i];
Toast toast = Toast.makeText(context, mesg.getDisplayMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
}
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
// retrieve the SMS message received ::
Object[] pdus = (Object[]) extras.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";
// Toast to display SMS ::
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
// Send SMS ::
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("put your phoneNumber(emulatorPort)", null, str, null, null);
}
}
}
--
Have nice time !
Don't forget to add permissions !