I am making an app that can send specific sms text to all the contacts the user have choosen with one click button.
The receivers will also installed that app in their phones. When that specifc sms is received in a phone which have installed the app, they will get alarm dialog to dismiss with audio playing from assets folder. (Even if the phone is locked or sleep, or app is not running[even the recent apps are cleared])
I have managed to send sms to all the choosen contacts with one click. And also read sms to ring alarm. My problem is,..... the alarm works only when the app is running. The app not work when the phone is locked, or sleep or, even the app is in recent apps. I have searched that a service should be use for that function. Please help me.
This is my Mainactivity.java
public class MainActivity extends AppCompatActivity {
EditText e1, e2;
Button b1;
private final static int SEND_SMS_PERMISSION_REQ=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.editText);
e2=findViewById(R.id.editText2);
b1=findViewById(R.id.button);
b1.setEnabled(false);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.RECEIVE_SMS)
!=PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.RECEIVE_SMS},1000);
}
if(checkPermission(Manifest.permission.SEND_SMS))
{
b1.setEnabled(true);
}
else
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_REQ);
}
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s1=e1.getText().toString();
// String s2=e2.getText().toString();
String s2="SMSBOOM";
if(!TextUtils.isEmpty(s1)&&!TextUtils.isEmpty(s2))
{
if(checkPermission(Manifest.permission.SEND_SMS))
{
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(s1,null,s2,null,null);
}
else {
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean checkPermission(String sendSms) {
int checkpermission= ContextCompat.checkSelfPermission(this,sendSms);
return checkpermission== PackageManager.PERMISSION_GRANTED;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode)
{
case SEND_SMS_PERMISSION_REQ:
if(grantResults.length>0 &&(grantResults[0]==PackageManager.PERMISSION_GRANTED))
{
b1.setEnabled(true);
}
break;
case 1000:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
finish();
}
break;
}
}
}
This is my receiver class
public class ReceiveSms extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras();
SmsMessage[] msgs;
String msg_from;
if(bundle != null){
try{
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]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
//https://stackoverflow.com/questions/14093404/android-alarmmanager-or-service
if(msgBody.equals("SMSBOOM")){
Toast.makeText(context, "From: "+msg_from + ", Msg: "+msgBody, Toast.LENGTH_SHORT).show();
this.abortBroadcast();
Intent i1 = new Intent(context, RingActivity.class);
i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i1.putExtra("num", msg_from);
i1.putExtra("msg", msgBody);
context.startActivity(i1);
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
This is my RingActivity
public class RingActivity extends Activity {
final Context context = this;
MediaPlayer mp = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ring);
Bundle extras = getIntent().getExtras();
String num = extras.getString("num");
String msg = extras.getString("msg");
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "I AM At Reciver\nsenderNum: "+num+", message: " + msg, duration);
toast.show();
AudioManager am =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(
AudioManager.STREAM_MUSIC,
am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
0);
// SmsManager smsManager = SmsManager.getDefault();
if(IsRingerSilent() || IsVibrate())
{
// smsManager.sendTextMessage(num, null, "Device turned to ringing mode.. && It's Ringing..", null, null);
AudioManager audioManager= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mp.setLooping(true);
try
{
AssetFileDescriptor afd;
afd = getAssets().openFd("ringtone.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
}
catch (IllegalStateException | IOException e)
{
e.printStackTrace();
}
}
else
{
// smsManager.sendTextMessage(num, null, "Device Ringing...", null, null);
AudioManager audioManager= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mp.setLooping(true);
try
{
AssetFileDescriptor afd;
afd = getAssets().openFd("ringtone.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
}
catch (IllegalStateException | IOException e)
{
e.printStackTrace();
}
}
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// Setting Dialog Title
alertDialogBuilder.setTitle("New SMS Boom");
// Setting Dialog Message
alertDialogBuilder.setMessage("Sender : "+num+"\n"+"Message : "+msg);
alertDialogBuilder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
if(mp.isPlaying())
{
mp.setLooping(false);
mp.stop();
}
dialog.cancel();
finish();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
//show dialog
alertDialog.show();
}
private boolean IsVibrate()
{
AudioManager audioManager = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
if(audioManager.getRingerMode()==AudioManager.RINGER_MODE_VIBRATE )
{
return true;
}
else
{
return false;
}
}
private boolean IsRingerSilent()
{
AudioManager audioManager = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
if(audioManager.getRingerMode()==AudioManager.RINGER_MODE_SILENT )
{
return true;
}
else
{
return false;
}
}
public boolean onKeyDown(int keycode, KeyEvent ke)
{
if(keycode==KeyEvent.KEYCODE_BACK)
{
if(mp.isPlaying())
{
mp.setLooping(false);
mp.stop();
}
finish();
}
return true;
}
}
Is there any idea to do this? Please help............