0

我正在尝试创建一个计划将 SMS 消息事件发送到您自己的手机的程序。由于某种原因,它不起作用。我创建了一个扩展 BroadcastReceiver 的 SmsAlarm 类,它的作用是设置/取消警报。这是课程:

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

@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, phoneNumber, messages[(int)(Math.random() * messages.length)], pi, null);
    }
}

public void SetAlarm(Context context) {
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, SmsAlarm.class);
    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);
}
//getters and setters after this method

为了在每次打开应用程序时使用相同的调度程序,我们在安装后第一次运行应用程序时序列化 SmsManager 对象。这些是 MainActivity 类中的 saveAlarm 和 loadAlarm 方法:

public void saveAlarm(Context context){
    try{
    FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
    ObjectOutputStream os = new ObjectOutputStream(fos);
    os.writeObject(manager);
    os.close();
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

public SmsAlarm loadAlarm(Context context){

    try{
    FileInputStream fis = context.openFileInput(FILENAME);
    ObjectInputStream is=new ObjectInputStream(fis);
    try {
        manager=(SmsAlarm) is.readObject();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    is.close();

    }
    catch(IOException e){
        e.printStackTrace();
    }
    return manager;
}

在 MainActivity.java 中,我有一个 SmsManager 的私有实例变量,并且在 onCreate 方法中,如果以前从未打开过应用程序,我将 manager 设置为新的 SmsManager:

if (!welcomeScreenShown) {
        manager = new SmsAlarm();
        saveAlarm(getApplicationContext());
        String whatsNewTitle = getResources().getString(R.string.whatsNewTitle);
        String whatsNewText = getResources().getString(R.string.whatsNewText);
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
                R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean(welcomeScreenShownPref, true);
        editor.commit(); // Very important to save the preference
    }

配置页面有两个时间选择器,让用户可以选择他们的时间限制和一个频率微调器,其中有多个选项用于接收消息的频率。对于测试,我使用了每一分钟。此代码在配置屏幕的 MainActivity 的 onCreate 中运行,但尚未显示:

if (configScreenShown)
    {
        manager = loadAlarm(getApplicationContext());
        //get the values from the SharedPreferences
        hour1 = setPrefs.getInt("hour1", 0);
        hour2 = setPrefs.getInt("hour2", 0);
        minute1 = setPrefs.getInt("minute1", 0);
        minute2 = setPrefs.getInt("minute2", 0);
        int freqInt = setPrefs.getInt("frequency", 0);
        frequencySet=freqInttoString(freqInt);

    }

它调用 loadAlarm 以检索上次打开应用程序时使用的 SmsAlarm。我认为 SmsAlarm.java 存在问题,我正在考虑摆脱该类并将所有逻辑放在 MainActivity 中。

清单文件:

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.RECEIVE_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>
   <receiver android:name=".SMSReceiver"> 
    <intent-filter>
    <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> 
        <data android:scheme="sms" /> 
        <data android:port="8901" /> 
    </intent-filter> 
    </receiver>
</application>

4

1 回答 1

0

AndroidManifest.xml如果您必须从手机发送消息,则可以将权限添加到文件中。

<uses-permission android:name="android.permission.SEND_SMS"/>

你加了这个吗?我想你可能会错过这个!

检查本教程

于 2013-02-09T23:02:17.377 回答