2013-07-31 55 views
1

我正在編寫一個應用程序,它接收用戶的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> 
+0

您是否調試過AlarmReceiver?在將來設置一分鐘的鬧鐘,在那裏放置一個斷點並在eclipse中連接調試器。看看方法是否被擊中。或者把一些日誌語句放在那裏。 然後,您可以確定問題是廣播接收器未被擊中,還是SMSManager未發送消息。 – athor

+0

我放置了一個斷點並遍歷代碼。 (我在alarmReciever中添加了Toasts以查看它是否正在運行....它看起來像方法從未被擊中 – user2639774

回答

1

您可以添加廣播接收器來接收SMS_SENT意圖。這將幫助您確定問題是否從未發送短信,而您的手機由於某種原因從未收到短信(例如,SMSC /路由問題等)

這裏是這種廣播接收機的示例:

 //---when the SMS has been sent--- 
     smsReceiver = new BroadcastReceiver(){ 
      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
       String result = ""; 

       switch (getResultCode()) 
       { 
       case Activity.RESULT_OK: 
        result = "Message sent";  
        break; 
       case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
        result = "Generic failure";       
        break; 
       case SmsManager.RESULT_ERROR_NO_SERVICE: 
        result = "No service"; 
        break; 
       case SmsManager.RESULT_ERROR_NULL_PDU: 
        result = "Null PDU"; 
        break; 
       case SmsManager.RESULT_ERROR_RADIO_OFF: 
        result = "Radio off"; 
        break; 
       } 
       Toast.makeText(context, result + ": " + message, Toast.LENGTH_LONG).show(); 
       Log.d(TAG, "<onReceive> received SMS sent intent: " + result); 

      } 
     }; 

那麼你註冊的意圖和使用發送短信:

registerReceiver(smsReceiver, new IntentFilter("SMS_SENT")); 
// SMS sent pending intent 
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0); 
// send the SMS 
sms.sendTextMessage(phoneNumberReciver, null, message, sentIntent, null); 
+0

對不起,我是這個新手。你是什麼意思「註冊意圖」? – user2639774

+0

當你註冊接收器時與新的意圖(「SMS_SENT」),這意味着接收將在SMS_SENT意圖被髮送時(當SMS消息被髮送時)被調用。 – droideckar

2

嘗試宣告你的清單中的接收器與一個完全合格的包名稱:

<receiver android:name="com.myapp.AlarmReciever"/> 

如果仍然不起作用,您可能需要指定接收方處理的操作,然後在創建鬧鐘意圖時將該操作設置爲意圖。 例如:

 <receiver android:name="AlarmReceiver" > 
      <intent-filter> 
       <action android:name="com.myapp.mybroadcast" /> 
      </intent-filter> 
     </receiver> 

Intent intentAlarm = new Intent(this, AlarmReciever.class); 
intent.setAction("com.myapp.mybroadcast");