无法安排发送Android的短信

时间:2013-02-10 17:30:49

标签: android broadcastreceiver alarmmanager telephonymanager smsmanager

我正在开发一个Android应用程序,需要设置以某种频率发送短信息的时间表。无论出于何种原因,我似乎无法收到消息以开始发送。

这是Schedule类:

public class SmsAlarm extends BroadcastReceiver {
private int hour1, minute1, hour2, minute2, frequency;

@Override
public void onReceive(Context context, Intent intent) {
    SmsManager manager = SmsManager.getDefault();
    TelephonyManager tMgr =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    String phoneNumber = tMgr.getLine1Number();
    Calendar c = Calendar.getInstance();
    int currentHour = c.get(Calendar.HOUR_OF_DAY);
    int currentMinute = c.get(Calendar.MINUTE);
    if (currentHour >= hour1 && currentHour <= hour2 && currentMinute >= minute1 && currentMinute <= minute2) {
        String[] messages = context.getResources().getStringArray(R.array.messages);
        manager.sendTextMessage(phoneNumber, null, messages[(int)(Math.random() * messages.length)], null, null);
    }
}

public void SetAlarm(Context context) {
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, SmsAlarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * frequency, pi); //millisecond * second * minutes
}

public void CancelAlarm(Context context) {
    Intent intent = new Intent(context, SmsAlarm.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}

}

我在mainactivity中设置了这个类顶部的所有私有变量,我还创建了一个这个类的序列化对象,我可以用来为每次使用保留警报。

如果我没有使用正确的权限,我会发布我的清单文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-permission android:name="android.permission.GET_TASKS"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:debuggable="true" >
    <activity
        android:name="com.example.myapp.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>

</application>

</manifest>

任何人都可以看到我无法发送消息的原因吗?

1 个答案:

答案 0 :(得分:0)

您似乎缺少在Manifest文件中注册接收器,并使用适当的操作字符串。如下所示。

 <receiver android:name=".SmsAlarm" > 
           <intent-filter>
              <action android:name="com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE" />
            </intent-filter>// can change name/action string as par ur requirement.
    </receiver>

为您的BroadcastReceiver

创建一个不同的.java文件

你需要在你的意图中设置相同的动作字符串,记住动作字符串在Manifest中必须相同,这里是intent.setAction(“com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE”);在java中也是。然后只会使接收器变干。

您的代码可以更改,如下所示。

Intent intent = new Intent(getApplicationContext(), to_call_when_alarm_goes_off.class);
intent.setAction("com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE");// added line

PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);

AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarms.cancel(pIntent);


alarms.setRepeating(
        AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis()+1000, 
        AlarmManager.INTERVAL_DAY, 
        pIntent);