我正在編寫一個應用程序,它接收用戶的EditText數據,並使用AlarmManager收集的數據設置警報,然後使用廣播接收器發送文本。我的小麪包上寫着警報已經設置好了,所以我知道數據正在被正確地收集,但我從未收到過任何文本。發生什麼事?如何使用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();
}
}
這裏是我的廣播reciever:
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>
您是否調試過AlarmReceiver?在將來設置一分鐘的鬧鐘,在那裏放置一個斷點並在eclipse中連接調試器。看看方法是否被擊中。或者把一些日誌語句放在那裏。 然後,您可以確定問題是廣播接收器未被擊中,還是SMSManager未發送消息。 – athor
我放置了一個斷點並遍歷代碼。 (我在alarmReciever中添加了Toasts以查看它是否正在運行....它看起來像方法從未被擊中 – user2639774