1

我正在编写一个应用程序,它从用户那里获取 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>
4

2 回答 2

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");
于 2013-08-01T20:52:38.930 回答
1

您可以添加一个广播接收器来接收 SMS_SENT 意图。这将帮助您确定问题是否是 SMS 从未发送,而您的手机由于某种原因(例如 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);
于 2013-07-31T22:53:56.950 回答