0

Alarm Manager を使用してタスクをスケジュールする(テキスト メッセージを送信する) 必要があり、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>
4

2 に答える 2

0

あなたのコードは、PendingIntent.getBroadcast() の「requestCode」パラメーターを期待して問題ないようです。

ゼロ以外の値で試してみてください:

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

修正されない場合は、問題の詳細をお知らせください。

于 2015-02-13T15:47:31.707 に答える
0

エラーが見つかったと思います。setRepeating メソッドで、トリガー パラメータを System.currentTimeMillis() に置き換えます。

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), delay, pendingIntent);
于 2015-02-13T16:07:21.370 に答える