Android SMS app Crashes when received sms contains lots of numbers - java

i have written the following code to get the received sms in phone
package com.example.recieveandsendsms;
public class IncomingSms extends BroadcastReceiver{
final SmsManager sms=SmsManager.getDefault();
#Override
public void onReceive(Context context, Intent intent) {
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 = msgs[i].getMessageBody().toString();
}
}
}
}
Following is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.recieveandsendsms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<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" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<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.recieveandsendsms.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="com.example.recieveandsendsms.IncomingSms">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
it works fine when I receive the normal message
means example Ex1: He was not angry or upset I was remember
was drawn on with us just a little thing.
when message in Ex1 comes it works fine
but when Ex2: UB 9000000000 9787878787-asghkajhsgk kjashgkas
but when sms like Ex2 compes the application crashes i am not able get what is wrong the message in Ex2
Please anyone can help me to tell where i am going wrong

The library version for reading and writing data was mismatch so it was crashing and it was not giving any error :(

Related

StartActivityForResult between 2 apps doesn't return any value to the first App

Let's say I have App A and App B.
In app A, I launch an intent to start App B in this way.
Intent intent = new Intent("com.example.myAppBAction");
Bundle bundle = new Bundle();
bundle.putString("anId", id);
intent.putExtras(bundle);
startActivityForResult(intent, myRequestCode);
Then in App B, I receive all my data and after doing some work I return a result this way.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent();
int codeWorkDone;//this is valued between 2 or 3
i.putExtra("FirstResult", anInteger);
i.putExtra("SecondResult", anotherInteger);
setResult(codeWorkDone, i);
finish();
}
Eventually in my onActivityResult I need to print all the extras put in App B, as it follows:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case myRequestCode:
StringBuilder result = new StringBuilder();
result.append(resultCode + "\n");
if (data != null){
Bundle b = data.getExtras();
if (b != null) {
for (String key : b.keySet()) {
result.append(key + " : " + (b.get(key) != null ? b.get(key) : "NULL"));
}
}
}
Log.d("esito", result.toString());
break;
}
}
The problem here is that the requestCode is correct, but I received back a resultCode which is 0 (remember that from app B is set between 2 or 3) and my data is null.
Any hint on this one?
EDIT:
App A Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appA"
android:versionCode="202040"
android:versionName="2.2.4" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:name=".application.MyAppAApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
>
<activity
android:name=".activity.MainActivity"
android:configChanges="orientation|screenSize"
android:theme="#style/AppTheme.NoActionBar"
android:launchMode="singleTask"
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.TestActivityToLaunchAppB"
android:label="#string/title_activity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustPan" >
</activity>
<meta-data
android:name="io.fabric.ApiKey"
android:value="xxx" />
</application>
</manifest>
App B Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.myApp.appB">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:name=".MyAppApplication"
android:allowBackup="false"
android:hardwareAccelerated="true"
android:icon="#drawable/xxhdpi"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#drawable/xxhdpi"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.cdd.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
<activity
android:name=".activity.MyLandingActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.example.myAppBAction" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>

Error While Sending Large Sentence As SMS using Array in java using default SMS service over SIM

I am having trouble on sending SMS from APP. I the App is working Fine on some Devices like Samsung, Nokia etc. But Not working on devices like OPPO, VIVO devices. All the devices I tested was SDK29 (ANDROID 10).
ps: I am newbie here
Below is my code Sombodey please Help me.
sendSms() method is Supposed to send the sms.
private void sendSms() {
String names = name_text.getText().toString();
String s1=phone_text.getText().toString();
String s2=("Hi "+names+", You have Successfully recharged ₹ " +d1 +" to your account on "+timeStamp+". Your current balance is ₹ "+amount+" . Kindly clear your balance on time without any due. \n - Thank you! ");
try {
if (!TextUtils.isEmpty(s1) && !TextUtils.isEmpty(s2)) {
if (checkPermission(Manifest.permission.SEND_SMS)) {
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> part = smsManager.divideMessage(s2); //this is line 629//
smsManager.sendMultipartTextMessage(s1, null, part, null, null);
} else {
Toast.makeText(UserDetailsActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(UserDetailsActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.wtf("userDetailsActivity","Error ", e);
e.printStackTrace();
}
}
nothing happens when this method is called on the device OPPO/VIVO
but works fine on samsung
What showing on the Run is
E/userDetailsActivity: Error
java.lang.SecurityException: getGroupIdLevel1
at android.os.Parcel.createException(Parcel.java:2085)
at android.os.Parcel.readException(Parcel.java:2053)
at android.os.Parcel.readException(Parcel.java:2001)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getGroupIdLevel1ForSubscriber(IPhoneSubInfo.java:1009)
at android.telephony.TelephonyManager.getGroupIdLevel1(TelephonyManager.java:4007)
at android.telephony.SmsMessage.hasEmsSupport(SmsMessage.java:1010)
at com.android.internal.telephony.SmsMessageBase.calcUnicodeEncodingDetails(SmsMessageBase.java:512)
at com.android.internal.telephony.gsm.SmsMessage.calculateLength(SmsMessage.java:1002)
at android.telephony.SmsMessage.fragmentText(SmsMessage.java:435)
at android.telephony.SmsManager.divideMessage(SmsManager.java:958)
at com.ancorp.aplo.activity.UserDetailsActivity.sendSms(UserDetailsActivity.java:629)
at com.ancorp.aplo.activity.UserDetailsActivity.access$700(UserDetailsActivity.java:61)
at com.ancorp.aplo.activity.UserDetailsActivity$11.onClick(UserDetailsActivity.java:607)
at android.view.View.performClick(View.java:7187)
at android.view.View.performClickInternal(View.java:7164)
at android.view.View.access$3500(View.java:813)
at android.view.View$PerformClick.run(View.java:27642)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:230)
at android.app.ActivityThread.main(ActivityThread.java:7752)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:508)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
this is my Manifest.xmml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ancorp.aplo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light">
<activity android:name=".activity.CollectionDetailsActivity"
android:parentActivityName=".activity.MainActivity"></activity>
<activity android:name=".activity.BoxDetailsActivity"
android:parentActivityName=".activity.MainActivity"/>
<activity android:name=".activity.PaymentDetailsActivity"
android:parentActivityName=".activity.MainActivity"/>
<activity
android:name=".activity.SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".activity.MainActivity" />
<activity android:name=".activity.AddPlanActivity" />
<activity
android:name=".activity.UserAddActivity"
android:label="Add Customer"
android:parentActivityName=".activity.MainActivity" />
<activity
android:name=".activity.UserDetailsActivity"
android:parentActivityName=".activity.MainActivity" />
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
<activity android:name=".activity.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE_PERMISSION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
I think the problem is with the way you declared the string.
Try without using the divide message function. Pass the arraylist directly to the sendMultipartTextMessage function

Android BroadcastReceiver not detecting SMS

I don't know what has gone wrong, but I'm having a difficult time with this one. The program is supposed to show a toast once a message is received. I have tried adding the priority in the manifest file, but it doesn't work.
The manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.comfy.textforwarder">
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="24" />
<uses-permission-sdk-23 android:name="android.permission.SEND_SMS" />
<uses-permission-sdk-23 android:name="android.permission.READ_SMS" />
<uses-permission-sdk-23 android:name="android.permission.RECEIVE_SMS" />
<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:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
<activity android:name=".Settings" />
<receiver
android:name=".MyReceiver"
android:enabled="false"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
Broadcast Receiver
public void onReceive(Context context, Intent intent) {
Log.d("RECEIVE", "Receiving SMS");
Bundle intentExtras = intent.getExtras();
if(intentExtras != null){
Object[] sms = (Object[]) intentExtras.get("pdus");
String msgStr = "";
for(int i = 0; i < sms.length; ++i){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBoddeh = smsMessage.getMessageBody().toString();
String addr = smsMessage.getOriginatingAddress();
msgStr += "SMS From " + addr + "\n";
msgStr += smsBoddeh + "\n";
}
Toast.makeText(context.getApplicationContext(), msgStr, Toast.LENGTH_SHORT).show();
Log.d("RECEIVE", msgStr);
}
}
Try enabling the same..
<receiver
android:name="com.smsforwarder.smsforwarder.SMSReceiver"
**android:enabled="true">**
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

Incoming SMS contents

I wish I could get the contents of an incoming sms but its not working ..
In fact, no toast will appear, and nothing appears in the log, I think the myreceiver is not initialized ...
So here is the manifest, the onCreate the first activity, and my receiver java file : Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.locateit.antholife.locateit">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<receiver android:name=".MyReceiver">
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<application
android:allowBackup="true"
android:icon="#mipmap/locate"
android:label="#string/app_nameprincipal"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Changelog"
android:label="#string/title_activity_changelog"
android:theme="#style/AppTheme.NoActionBar" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBfmF3WWxsPhufZR5keiDNRy-33hJI1rvM" />
<activity
android:name=".lequipe"
android:label="#string/nomequipe" />
<activity
android:name=".Setting"
android:label="#string/title_activity_setting" />
<activity android:name=".MDPinterne" />
<activity android:name=".Bluetooth"></activity>
</application>
</manifest>
onCreate :
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
MyReceiver sms = new MyReceiver();
Log.v("aha3", "smslancer");
and java file :
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Bundle extra = intent.getExtras();
if (extra != null) {
Object[] pdus = (Object[]) extra.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.v("Ch", "Marche1");
for (int i = 0; i < messages.length; i++) {
Log.v("Ch", "Marche2");
final String messageBody = messages[i].getMessageBody();
final String phoneNumber = messages[i].getDisplayOriginatingAddress();
Toast.makeText(context, "Expéditeur:" + phoneNumber, Toast.LENGTH_LONG).show();
Toast.makeText(context, "Message : " + messageBody, Toast.LENGTH_LONG).show();
}
}
}
}
}
Move your receiver XML in your manifest to within the application tags.
<application
android:allowBackup="true"
android:icon="#mipmap/locate"
android:label="#string/app_nameprincipal"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
.......
<receiver android:name=".MyReceiver">
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>

Android - Starting service by booting and wifi on

I am sorry for my bad english, I am from germany.
I try to explain my problem.
I'll start a service, if the smartphone booted and is internet connection enabled. I hope you can understand me.
This is my Autostart
public class Autostart extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
try {
context.startService(new Intent(context, SpyService.class));
} catch (Exception ex) {
}
}
}
}
and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.caglar.spyme"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".Autostart" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity android:name=".StartService">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".SpyService"
android:enabled="true" />
</application>
</manifest>
If i restart my phone and open the wlan, the service is not started. Why?
use this code in your broadcast.
public class Autostart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();
if (isConnected)
// start your service
}
}

Categories