Java Android : Subclass, Call method from main activity class caused NullPointerException - java

i want to call function from subclass to main activity class.
here is my source code :
SMS.java
public class SMS extends ListActivity {
public void testerr(String kata) {
Toast.makeText(getBaseContext(), "test coyyyy="+kata, Toast.LENGTH_LONG).show();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
SMSReceiver.java
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
try {
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
SMS sss = new SMS(); ---> call the main class
sss.testerr("try the words"); ---> call method from main class
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
}
}
catch(NullPointerException ex){
Toast.makeText(context, "penyakite neng kene:"+ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
my program run after it receive new text message, and i always get NullPointerException when call that sss.testerr("my example words");
any clue guys?
thankyou so much for your help

You should not instantiate the Activity with it's constructor, either start it with an intent, or, if the Activity is already up, make it implement the broadcast receiver.
If you only want to show toast from your receiver, you can use the answer for this question

Related

Sending Android SMS BroadcastReceiver data to Flutter through EventChannel

I am trying to listen for any incoming SMS messages on an Android device and then showing a toast in my Flutter app whenever an SMS is received. I am connecting to Flutter through EventChannel and detecting SMS using a BroadcastReceiver. How do I send an events.success(message) whenever my broadcast receiver detects an SMS?
I tried adding the BroadcastReceiver directly inside the EventChannel but that did not work. The flutter SMS package also doesn't seem to work.
This is what my MainActivity looks like:
public class MainActivity extends FlutterActivity{
public static final String STREAM = "com.myapp.thisapp/stream";
public static final String TAG = "THIS IS A MESSAGE: ";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new EventChannel(getFlutterView(), STREAM).setStreamHandler(
new EventChannel.StreamHandler() {
#Override
public void onListen(Object args, final
EventChannel.EventSink events) {
//Send events.success() when SMS received
Log.w(TAG, "adding listener");
}
#Override
public void onCancel(Object args) {
Log.w(TAG, "cancelling listener");
}
}
);
}
}
And this is the code for my BroadcastReceiver:
public class IncomingSmsBroadcastReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(final Context context, final Intent intent) {
if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
final SmsMessage smsMessage = extractSmsMessage(intent);
processMessage(context, smsMessage);
}
}
private SmsMessage extractSmsMessage(final Intent intent) {
final Bundle pudsBundle = intent.getExtras();
final Object[] pdus = (Object[]) pudsBundle.get("pdus");
final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
return smsMessage;
}
private void processMessage(final Context context, final SmsMessage smsMessage) {
//TODO: Send message to event channel
}
}
Whenever the BroadCastReceiver encounters an SMS, I want the content of the message to be sent to the EventChannel which will send the message text to the Flutter front-end. How do I do this?
Based on your first comment Laksh22 YES it can be possible.
Create a constructor for your 'Activity' like this :
_yourclassnameState() {
platform.setMethodCallHandler(JavaMethodHandler);
}
and then implement a handler for response
Future<dynamic> JavaMethodHandler(MethodCall methodcall) async
{
switch(methodcall.method)
{
case 'SMSRecived':
print('DataRecived is' + methodcall.arguments);
break;
default:
break;
}
}
then in your 'BroadcastReceiver' use this code to call a flutter method :
MethodChannel channel =new MethodChannel(view,CHANNEL);
channel.invokeMethod("SMSRecived",args, new MethodChannel.Result() {
#Override
public void success(Object o) {
System.out.println(o);
}
#Override
public void error(String s, String s1, Object o) {
}
#Override
public void notImplemented() {
}
});
Don't forget about CHANNEL. It should be the same both side.
Don't forget about manifest file.
For receiving SMS while your app is closed use 'ForegroundService'.Unfortunately Flutter dosen't support ForegroundService yet. you should implement it in Java.
This is what you should be using in MainActivity, store the Result
public class MainActivity extends FlutterActivity{
public static final String STREAM = "com.myapp.thisapp/stream";
public static final String TAG = "THIS IS A MESSAGE: ";
public Result resultLater;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
#Override
public void onMethodCall(MethodCall call, Result result) {
//store the reference for later access
resultLater = result;
}
});
}
public class IncomingSmsBroadcastReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(final Context context, final Intent intent) {
if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
final SmsMessage smsMessage = extractSmsMessage(intent);
processMessage(context, smsMessage);
}
}
private SmsMessage extractSmsMessage(final Intent intent) {
final Bundle pudsBundle = intent.getExtras();
final Object[] pdus = (Object[]) pudsBundle.get("pdus");
final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
return smsMessage;
}
}
private void processMessage(final Context context, final SmsMessage smsMessage) {
//here send back result, like this
if(smsMessage.getMessageBody()!=null){
result.success(smsMessage.getMessageBody());
}else{
result.error("Error", "Sms not found", null);
}
}
}
now make the sms broadcast sub class in your main activity and declare below. and access the result when you get the sms. And from your flutter side just make channel call as usual.
Follow the reference for docs sample.
Register your broadcast receiver in EventChannel's onListen method
override fun onListen(arguments: Any?, eventSink: EventSink) {
val receiver = IncomingSmsBroadcastReceiver()
receiver.setListener(object : SmsReceiveListener() {
override fun onSmsReceive(sms: String?) {
eventSink.success(sms)
}
})
val filter = IntentFilter(IncomingSmsBroadcastReceiver.SMS_RECEIVED)
context.registerReceiver(receiver, filter)
}
A SmsReceiveListener is registered to IncomingSmsBroadcastReceiver to listen to the received message.
For more detail check the below URL:-
https://medium.com/cashify-engineering/event-channel-to-listen-to-broadcast-events-from-android-43a813672896

how to show received sms in textview instead of toast

I am currently working on a simple android application to read an SMS and print it in TexView instead of Toast. but in receiver activity, we do not initialise "findbyid" so we can't print the SMS in textView. now I am showing SMS in Toast to test but I do not want it in a toast. I am also read questions/answer and also other articles but can't find what I want.
receiver activity,
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class read_sms extends BroadcastReceiver {
// Get the object of SmsManager
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
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("SmsReciver", "senderNum: " + senderNum + ", message: " + message);
//ourSMS.getSmsDetails(senderNum, message);
// Show SMS notification
//Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, Toast.LENGTH_LONG).show();
if(message.equals("Milind")){
Toast.makeText(context, "sms matched", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "not matched", Toast.LENGTH_LONG).show();
}
} // end of for loop
} // bundle
} catch (Exception e) {
// TODO: handle exception
Log.e("SmsReciver", "Exception smsReciver" + e);
}
}
}
my home activity is empty because I can't find which code placed here.
public class home extends AppCompatActivity {
TextView SMS_textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
SMS_textview = (TextView) findViewById(R.id.sms_text);
}
}
change that TextView to
public static TextView SMS_textview;
add method
public void recivedSms(String message)
{
try
{
SMS_textview.setText(message);
}
catch (Exception e)
{
}
}
in read_sms class add the following code when you recieve sms
home Sms = new home();
Sms.recivedSms(message );
Register your Receiver in your activity whenever you Receive a text in receiver
use this code in your Receiver class
Intent broadcastIntent = new Intent();
broadcastIntent.putExtra("your key", your Value);
broadcastIntent.setAction("link from you have recieve a text");
context.sendBroadcast(broadcastIntent);
after this register a broadcast Receiver in your activity like this
private void registerSmsReciever() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("link from you have recieve a text");
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive", "Sms recieved in progress");
String message= intent.getStringExtra("key");
textView.setText(intent.getStringExtra("key"));
}
};
registerReceiver(broadcastReceiver, intentFilter);
}

How to insert TextView in Activity from other non activity class Android?

I need to set TextView object of Main3Activity (Activity class) from IncomingSms (Non activity class).
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
#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 senderNum = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
if (message.equalsIgnoreCase("FIRE DETECTED")) {
//Problem start here: I cant able to set TextView object of Main3Activity
Main3Activity main3Activity = new Main3Activity();
TextView tv = (TextView) main3Activity.findViewById(R.id.firealert);
tv.setText(message);
//Problem end here
Log.i("SmsReceived", "senderNum: " + senderNum + "; message: " + message);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, message, duration);
toast.show();
}
}
}
} catch(Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
create an interface class save it as java class then
public interface SmsListener {
public void messageReceived(String messageText);
}
and
final SmsManager sms = SmsManager.getDefault();
private static SmsListener mListener;
#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 senderNum = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
if (message.equalsIgnoreCase("FIRE DETECTED") ){
mListener.messageReceived(message); //add this
Log.i("SmsReceived", "senderNum: "+ senderNum + "; message: " + message);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, message, duration);
toast.show();
}
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
public static void bindListener(SmsListener listener) {
mListener = listener;
}
And in your Main3Activity onCreate method
TextView tv=(TextView) main3Activity.findViewById(R.id.firealert);
SmsReceiver.bindListener(new SmsListener() {
#Override
public void messageReceived(String messageText) {
Log.d("Text",messageText);
tv.setText(messageText);
}
});
You can't create the activity instance like that.
Solution:
1. Create a broadcast receiver in your activity and register for a custom intent.
2. Send a custom broadcast intent from your sms receiver. So basically that will reach activity receiver and then you can simply update the text there.
In case your activity is not started, simply create a activity intent and pass the sms text as part of the intent extras.

How to pass data from Receiver to Activity using interface?

I'm trying to pass the data from the brodcast receiver to the activity to display it on the view using an interface, but, it's not working.Here are my files,MainActivity.java
public class MainActivity extends AppCompatActivity implements OtpPassing {
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onReceive(String msg) {
Toast.makeText(this, "o" + msg, Toast.LENGTH_SHORT).show();
textView = (TextView) findViewById(R.id.textView);
textView.setText("OTP: " + msg);
}
}
OtpPassing.java
public interface OtpPassing {
public void onReceive(String msg);
}
Receiver.java
public class Receiver extends BroadcastReceiver {
// Get the object of SmsManager
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);
String otp = getOtp(message);
Toast.makeText(context, otp, Toast.LENGTH_LONG).show(); //This is working fine
OtpPassing otpPassing = new MainActivity();
otpPassing.onReceive(otp); //This is not working
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
private String getOtp(String message) {
String otp = null;
int index = message.indexOf("otp:");
if(index != -1){
int start = index + 5;
otp = message.substring(start, start+4);
}
return otp;
}
}
The receiver is woking(it's displaying the message on a Toast) but why is displaying the message on the screen via MainActivity not working?
You can create a broadcast receiver as a data member of your activity. You register for it in onCreate and unregister in onDestroy (or onStart and onStop).
That way the receiver has a reference to the parent activity and you can do whatever.
public class MainActivity extends AppCompatActivity implements OtpPassing {
private TextView textView;
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//Access the parent activity as follows
MainActivity.this.textView.setText("...");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//instantiate and register the receiver
setContentView(R.layout.activity_main);
}
#Override
protected void onDestroy() {
// Unregister the receiver here
super.onDestroy();
}
Here better way (I suppose) would be to use Android Intents
in your receiver, something like this
Intent intent = new Intent(context,MainActivity.class);
intent.putExtra(.....);
startactivity(intent);
now in your MainActivity, override onNewIntent(). Here check for the 'extras' you had passed from the receiver.
Please note, this might result in having multiple MainActivities running in your phone if your "older" MainActivity was not killed by the time this intent was passed via receiver. To overcome this, use 'singleTop' or similar flags in your Manifest file

Receive data and show them in a Textview

The data i receive with my smartphone through Bluetooth LE occurs in this method in my service class
public void onCharacteristicRead(BluetoothGattCharacteristic charac, int status)
{
UUID charUuid = charac.getUuid();
Bundle mBundle = new Bundle();
Message msg = Message.obtain(mActivityHandler, HRP_VALUE_MSG);
Log.i(TAG, "onCharacteristicRead");
if (charUuid.equals(BODY_SENSOR_LOCATION))
mBundle.putByteArray(BSL_VALUE, charac.getValue());
msg.setData(mBundle);
msg.sendToTarget();
}
The Handler in the activity class is contructed like this:
private Handler mHandler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case HRPService.HRP_VALUE_MSG:
Log.d(TAG, "mHandler.HRP_VALUE_MSG");
Bundle data1 = msg.getData();
final byte[] bslval = data1.getByteArray(HRPService.BSL_VALUE);
runOnUiThread(new Runnable()
{
public void run()
{
if (bslval != null)
{
try
{
Log.i(TAG, "BYTE BSL VAL =" + bslval[0]);
TextView bsltv = (TextView) findViewById(R.id.BodySensorLocation);
bsltv.setText("\t" + mContext.getString(R.string.BodySensorLocation)
+ getBodySensorLocation(bslval[0]));
}
catch (Exception e)
{
Log.e(TAG, e.toString());
}
}
}
});
default:
super.handleMessage(msg);
}
}
};
Can someone tell med the relationship between those two methods ?. I receive an array of data from the remote device, and i want the data to be shown on the Textview "bsltv". How do i do this ?.
Thanks in advance
I created a class called BLEGattCallback which receives the updates. In this class I implemented the interface which you are mentioned. To receive and display the data I just
send and Intent with the needed informations put into a Bundle. Just create a BroadcastReceiver in your Activity and register it for the Action. Read all data from the Bundle and display it by calling TextView.setText(String).
The UUIDManager is just a class I created to translate the Characteristic into a readable String.
#Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
if(status == BluetoothGatt.GATT_SUCCESS) {
Log.d(CLASSNAME, "onCharacteristicRead for Characteristic: " + characteristic.getUuid().toString());
Log.d(CLASSNAME, "onCharacteristicRead for: " + UUIDManager.getCharacteristic(characteristic.getUuid().toString()));
Intent intent = new Intent();
intent.setAction(ACTION_READ_DATA_AVAILABLE);
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data) {
stringBuilder.append(String.format("%02X ", byteChar));
}
Bundle extras = new Bundle();
extras.putString(EXTRA_CHARACTERISTIC_UUID, characteristic.getUuid().toString());
extras.putString(EXTRA_STRING_DATA, stringBuilder.toString());
extras.putByteArray(EXTRA_RAW_DATA, characteristic.getValue());
extras.putString(EXTRA_DEVICE_ADDRESS, gatt.getDevice().getAddress());
intent.putExtras(extras);
}
m_Context.sendBroadcast(intent);
} else {
Log.e(CLASSNAME, "onCharacteristicRead was not successfull!");
}
}

Categories