安排一个不能用于发送文本消息的任务,特别是重复的时间间隔

时间:2015-02-13 15:27:33

标签: java android alarmmanager android-alarms repeatingalarm

我需要使用闹钟管理器安排任务(发送短信),我需要以1分钟的间隔重复发送

我真的很感激有关这方面的任何帮助。提前谢谢。

这是我写的代码,

  

的AndroidManifest.xml

     

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SEND_SMS"> </uses-permission>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".SMSScheduler"
        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>

<service android:name=".MyAlarmService" android:enabled="true" />

<receiver android:process=":remote" android:name=".MyReceiver" /> 
     

     

SMSScheduler.java

package com.example.smsmessaging;

public class SMSScheduler extends Activity {

    Button btnSendSMS;

    Calendar calendar = Calendar.getInstance();

    private PendingIntent pendingIntent;

    public void startNotification () {

        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(2015, 1, 13, 10, 12, 00); calendar.getTime();

        Intent myIntent = new Intent(SMSScheduler.this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(SMSScheduler.this, 0, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        long trigger = calendar.getTimeInMillis();
        long delay = 1*60*1000; // 1 minute delay needed

        // TEST CODE
        String time = calendar.getTime().toString();
        Log.v("TEST", time); 
        Toast.makeText(getBaseContext(), time, Toast.LENGTH_LONG).show();

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, trigger, delay, pendingIntent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smsscheduler);

        btnSendSMS = (Button)findViewById(R.id.buttonSchedule);

        btnSendSMS.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startNotification();
            }
        });
    }
}
  

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {
      int count = 1;
      Map<Integer, String> oMessages = new HashMap<Integer, String>();

        public void updateMessages() {

            oMessages.put(1, "TEST 1");
            oMessages.put(2, "TEST 2");
            oMessages.put(3, "TEST 3");
            oMessages.put(4, "TEST 4");
        }

        public String getMessage(Integer i) {
            return oMessages.get(i);
        }


    @Override
    public void onReceive(Context context, Intent intent)
    {
            // TODO Auto-generated method stub

        updateMessages();                 
        String phoneNumberReciver="123456789";// phone number to which SMS to be send
        String message=getMessage(count);// message to send

        SmsManager sms = SmsManager.getDefault(); 
        sms.sendTextMessage(phoneNumberReciver, null, message, null, null);

        // Show the toast  like in above screen shot
        Log.v("TEST", "Alarm Triggered and SMS Sent");
        Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();
     }
}
  

SMSMessagingManifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smsmessaging"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.SEND_SMS"> </uses-permission>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SMSScheduler"
            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>

    <service android:name=".MyAlarmService" android:enabled="true" />

    <receiver android:process=":remote" android:name=".MyReceiver" /> 


</manifest>

2 个答案:

答案 0 :(得分:0)

您的代码似乎可以预期&#34; requestCode&#34; PendingIntent.getBroadcast()

中的参数

您可以尝试使用非零值:

Intent myIntent = new Intent(SMSScheduler.this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(SMSScheduler.this, 0, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

如果没有解决,请向我们提供有关错误的详细信息。

答案 1 :(得分:0)

我想我发现了错误。在setRepeating方法中,用System.currentTimeMillis()

替换trigger参数
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), delay, pendingIntent);