0

こんにちは、私のアプリケーションでは、バックグラウンド サービスとアラーム マネージャーを使用して、動的な間隔で WebView を実行しています。この時間間隔をアラーム マネージャに入力します。

ここで、私がしようとしていること

private void loadWebview() {
    try {
        loadPrefValues();

        Calendar cur_cal = Calendar.getInstance();
        cur_cal.setTimeInMillis(System.currentTimeMillis());
        Intent webViewIntent = new Intent(MyService.this, XorWebView.class);
        webViewIntent.putExtra("url", mStr);
        webViewIntent.putExtra("duration", mDurationStr);
        webViewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        pintent = PendingIntent.getActivity(MyService.this, 0,
                webViewIntent, 0);

        alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        if (mIntervalStr.equals("1 minute")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 1 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("2 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 2 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("5 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 5 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("10 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 10 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("30 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 30 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("60 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 60 * 60 * 1000, pintent);
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    System.out.println("Service.onStart()" + "Url:" + mStr
            + "Send to WebActivity");
}

このメソッドは、最初に指定された間隔でのみ実行されます。コードの何が問題なのですか?

編集#1

MainActivity では、間隔にオン/オフ スイッチ ボタンとリストビューを使用しています。サービスはこちら。

このサービス クラスでは、Alarmmanager を使用して間隔を呼び出します。

ユーザーがメインアクティビティの間隔を変更したときに、その間隔がアラームマネージャーで更新されるときに正確にしたいこと。

私のサービスクラス

public class MyService extends Service {

BroadcastReceiver mReceiver = null;
String mStr, mIntervalStr, mDurationStr;
PendingIntent pintent;
AlarmManager alarm;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    Toast.makeText(MyService.this, "Service Created", Toast.LENGTH_SHORT)
            .show();
    System.out.println("Service.onCreate()");

}

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);

    Toast.makeText(MyService.this, "Service started", Toast.LENGTH_SHORT)
            .show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    // Let it continue running until it is stopped.
    try {
        loadWebview();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    Toast.makeText(MyService.this, "Service command started",
            Toast.LENGTH_SHORT).show();
    return START_STICKY;
}


private void loadWebview() {
    try {
        loadPrefValues();

        Calendar cur_cal = Calendar.getInstance();
        cur_cal.setTimeInMillis(System.currentTimeMillis());
        Intent webViewIntent = new Intent(MyService.this, XorWebView.class);
        webViewIntent.putExtra("url", mStr);
        webViewIntent.putExtra("duration", mDurationStr);
        webViewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        pintent = PendingIntent.getActivity(MyService.this, 0,
                webViewIntent, 0);

        alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pintent);
        if (mIntervalStr.equals("1 minute")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,cur_cal.getTimeInMillis(),
                    1 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("2 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 2 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("5 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 5 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("10 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 10 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("30 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 30 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("60 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 60 * 60 * 1000, pintent);
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    System.out.println("Service.onStart()" + "Url:" + mStr
            + "Send to WebActivity");
}

private void loadPrefValues() {
    SharedPrefManager.Init(MyService.this);
    SharedPrefManager.LoadFromPref();
    String s1 = SharedPrefManager.getsUrl().toString();
    String s2 = SharedPrefManager.getsInterval().toString();
    String s3 = SharedPrefManager.getsDuration().toString();

    mStr = s1;
    mIntervalStr = s2;
    mDurationStr = s3;


}


@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if (mReceiver != null)
        unregisterReceiver(mReceiver);
    alarm.cancel(pintent);
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    System.out.println("Service.onDestroy()");
}

}

4

2 に答える 2

0

以下のことが起こっています。

->アクティビティからalarmManagerを更新します。->将来の使用のために現在の時間間隔を設定に保存します。->再起動ブロードキャスト レシーバーを実装します。デバイスが再起動すると呼び出されます。→reboot関数にalarmManagerを設定。設定に保存されている時間間隔を使用します。

このコードをアクティビティにコピーします。

       listview.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View v, int position,
                            long id) {
                        loadWebView(user's new selected minutes);
                    }
});

AlarmManager を設定するコードは次のとおりです

     private void loadWebview(String minutes) {
    try {
        loadPrefValues();



        saveCurrentTimeInterval(minutes);




        Calendar cur_cal = Calendar.getInstance();
        cur_cal.setTimeInMillis(System.currentTimeMillis());
        Intent webViewIntent = new Intent(MyService.this, XorWebView.class);
        webViewIntent.putExtra("url", mStr);
        webViewIntent.putExtra("duration", mDurationStr);
        webViewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        pintent = PendingIntent.getActivity(MyService.this, 0,
                webViewIntent, 0);

        alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        if (minutes.equals("1 minute")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 1 * 60 * 1000, pintent);
        }
        if (minutes.equals("2 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 2 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("5 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 5 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("10 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 10 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("30 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 30 * 60 * 1000, pintent);
        }
        if (mIntervalStr.equals("60 minutes")) {
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                    cur_cal.getTimeInMillis(), 60 * 60 * 1000, pintent);
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

}

private void saveCurrentTimeInterval(String minute) { SharedPreferences pref = getSharedPreferences("time", Context.MODE_PRIVATE); SharedPreferences.Editor エディター = pref.edit(); eidtor.putString("間隔", 分); editor.commit(); }

Now ReBoot リスナー

import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;    

public class BootCompleteReceiver extends BroadcastReceiver {   

    @Override  
    public void onReceive(Context context, Intent intent) {  

        SharedPreferences pref = context.getSharedPreferences("time", Context.MODE_PRIVATE);
        setAlarmManager(pref.getString("interval", ""));   

    }  

    private void setAlarmManager(String minutes) {
        try {
            loadPrefValues();

            Calendar cur_cal = Calendar.getInstance();
            cur_cal.setTimeInMillis(System.currentTimeMillis());
            Intent webViewIntent = new Intent(MyService.this, XorWebView.class);
            webViewIntent.putExtra("url", mStr);
            webViewIntent.putExtra("duration", mDurationStr);
            webViewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            pintent = PendingIntent.getActivity(MyService.this, 0,
                    webViewIntent, 0);

            alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            if (minutes.equals("1 minute")) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                        cur_cal.getTimeInMillis(), 1 * 60 * 1000, pintent);
            }
            if (minutes.equals("2 minutes")) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                        cur_cal.getTimeInMillis(), 2 * 60 * 1000, pintent);
            }
            if (mIntervalStr.equals("5 minutes")) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                        cur_cal.getTimeInMillis(), 5 * 60 * 1000, pintent);
            }
            if (mIntervalStr.equals("10 minutes")) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                        cur_cal.getTimeInMillis(), 10 * 60 * 1000, pintent);
            }
            if (mIntervalStr.equals("30 minutes")) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                        cur_cal.getTimeInMillis(), 30 * 60 * 1000, pintent);
            }
            if (mIntervalStr.equals("60 minutes")) {
                alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                        cur_cal.getTimeInMillis(), 60 * 60 * 1000, pintent);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

}

あなたのxmlで。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootCompleteReceiver">  
            <intent-filter>     
                <action android:name="android.intent.action.BOOT_COMPLETED"/>  
                <category android:name="android.intent.category.DEFAULT" />  
            </intent-filter>  
        </receiver>
于 2013-11-08T13:12:23.283 に答える
0

最初に以前の pendingIntent をキャンセルしてから、新しく設定する必要があります。

この行の後のコードで。

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

置く

alarm.cancel(pintent);
于 2013-11-08T10:59:14.843 に答える