我正在编写一个应用程序,它从用户那里获取 EditText 数据,并使用使用 AlarmManager 收集的数据设置警报,然后使用广播接收器发送文本。我的小吐司说明警报已设置好,所以我知道数据收集正确,但我从未收到短信。怎么了?
这是我收集数据并设置警报的主要活动:
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void scheduleAlarm(View V)
{
EditText timeField = (EditText) findViewById(R.id.editText1);
int time = Integer.parseInt(timeField.getText().toString());
EditText dayField = (EditText) findViewById(R.id.editText2);
int day = Integer.parseInt(dayField.getText().toString());
EditText monthField = (EditText) findViewById(R.id.editText3);
int month = Integer.parseInt(monthField.getText().toString());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, time);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intentAlarm = new Intent(this, AlarmReciever.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
Toast.makeText(this, ("Alarm scheduled for " + month + "/" + day + " at " + time + "pm"), Toast.LENGTH_LONG).show();
}
}
这是我的广播接收器:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
public class AlarmReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
String phoneNumberReciver="0000000000"; //my phone number usually entered here
String message="Hi I will be there later, See You soon";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumberReciver, null, message, null, null);
}
}
我还确保使用以下权限编辑我的清单:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<receiver android:name=".AlarmReciever"/>
但这是整个 xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bpa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bpa.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=".AlarmReciever"/>
</application>
</manifest>