我正在尝试实施短信调度应用。在选择用户需要安排短信的时间后,我正在呼叫服务。当应用程序在前台和后台运行时,一切正常。但是在关闭应用程序时无法正常工作。我也使用了报警管理器和报警管理器一样。我还在清单文件中声明了“Process”属性。任何帮助将不胜感激。
这些是我的代码文件。看看。
1. MainActivity
public class MainActivity extends AppCompatActivity implements
View.OnClickListener{
Button mButton;
EditText mPhoneNumber;
EditText mMessage;
String message=null;
String phoneNumber=null;
Button alarmButton;
int number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.send_button);
mPhoneNumber = (EditText) findViewById(R.id.phone_number);
mMessage = (EditText) findViewById(R.id.message);
message=mMessage.getText().toString();
phoneNumber=mPhoneNumber.getText().toString();
alarmButton=(Button)findViewById(R.id.alarm_button);
alarmButton.setOnClickListener(this);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.send_button:
if(!message.equals(null)&&!phoneNumber.equals(null)){
sendSS(mMessage.getText().toString(),mPhoneNumber.getText().toString());
}
break;
case R.id.alarm_button:
int currentHour,currentMinute;
Calendar ca = Calendar.getInstance();
currentHour = ca.get(Calendar.HOUR_OF_DAY);
currentMinute = ca.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
sendPickedTime(hourOfDay,minute);
}
}, currentHour, currentMinute, true);
tpd.show();
break;
}
}
public void sendSS(String messageText, String contactNumber){
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS},1);
SmsManager smsManager=SmsManager.getDefault();
Log.i("contactNumber",contactNumber);
Log.i("messageText",messageText);
smsManager.sendTextMessage(contactNumber,null,messageText,sentPI,deliveredPI);
}
public void sendPickedTime(final int mHour, final int mMinute){
int mYear,mMonth,mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar messageDT=Calendar.getInstance();
messageDT.set(year, monthOfYear , dayOfMonth);
messageDT.set(Calendar.HOUR_OF_DAY, mHour);
messageDT.set(Calendar.MINUTE, mMinute);
messageDT.set(Calendar.SECOND, 0);
sendMessage(messageDT);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
public void sendMessage(Calendar cal){
//Intent intentAlarm = new Intent(this, AlarmReceiver.class);
Intent intent = new Intent(MainActivity.this, NewService.class);
String message=mMessage.getText().toString();
String phoneNumber=mPhoneNumber.getText().toString();
if(!message.equals(null)&&!phoneNumber.equals(null)){
intent.putExtra("contactNumber",phoneNumber);
intent.putExtra("messageContent",message);
}
PendingIntent pIntent = PendingIntent.getService(MainActivity.this, 0, intent,0);
// create the object
Toast.makeText(this, "We will takecare of your sms :)", Toast.LENGTH_LONG).show();
startService(new Intent(getApplicationContext(),NewService.class));
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), pIntent);
//set the alarm for particular time
//alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), PendingIntent.getBroadcast(this,0, intentAlarm, 0));
}
}
public class NewService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i("Service is created","Service is created");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Message is end","Message is end");
SmsManager smsManager=SmsManager.getDefault();
if(intent.hasExtra("contactNumber")&&intent.hasExtra("messageContent")) {
smsManager.sendTextMessage(intent.getExtras().getString("contactNumber"), null, intent.getExtras().getString("messageContent"), null, null);
}else{
//smsManager.sendTextMessage("+918939376946", null, "Message failed", null, null);
}
return START_STICKY;
}
}
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SmsManager smsManager=SmsManager.getDefault();
// Log.i("contactNumber",contactNumber);
//Log.i("messageText",messageText);
smsManager.sendTextMessage("+91*********",null,"nvjkrvk",null,null);
Log.i("Received Alarm","Alarm Trigerred");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.*****.sendingsms"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SEND_SMS"></uses- permission>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<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>
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote"></receiver>
<service android:name=".NewService"
android:exported="true"
android:process=":ServiceProcess"
android:enabled="true"
/>
</application>
activit_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.*****.sendingsms.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Message"
android:id="@+id/message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/send_button"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter phone no."
android:id="@+id/phone_number"
android:layout_below="@+id/message"
android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Alarm"
android:layout_below="@id/send_button"
android:id="@+id/alarm_button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>