5

この機能をアプリに追加して、ユーザーがアプリケーションの開始時間を設定し、その時点でアプリケーションを開始できるようにしたいと考えています。ブロードキャスト レシーバーを使用して、ユーザー固有の時間にアプリを開くにはどうすればよいですか。これがアンドロイドで可能かどうかわかりませんか?アイデアがあれば共有してください。ここに主な活動コードがあります

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv1=(ListView)findViewById(R.id.listView);
    final ImageView splashImage = (ImageView) findViewById(R.id.imageView1);
    splashImage.setBackgroundResource(R.layout.splash);
    AnimationDrawable  splashAnimation = (AnimationDrawable) splashImage.getBackground();
    splashImage.onWindowFocusChanged(true);
    splashAnimation.start();    
    AlarmManager am = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
    Date futureDate = new Date(new Date().getTime() + 86400000);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 1);// start app in 1 min again
    futureDate.setHours(0);
    futureDate.setMinutes(0);
    futureDate.setSeconds(20);
    Intent intent = new Intent(getBaseContext(), MyAppReciever.class);
    PendingIntent sender = PendingIntent.getBroadcast(getBaseContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis() , sender);}}

これは受信者クラスのコードです

class MyAppReciever extends BroadcastReceiver{ public void onReceive(Context context,Intent intent) {
startActivity(new Intent(context, Main_Activity.class));
}private void startActivity(Intent intent) {
// TODO Auto-generated method stub}}

この行をマニフェストに追加しました

<receiver  android:process=":remote" android:name="MyAppReciever"></receiver>

今私のブロードキャストトリガーですが、このエラーが発生しました

10-02 17:56:27.735: E/AndroidRuntime(9020): java.lang.RuntimeException: Unable to instantiate receiver com.example.testgui.MyAppReciever: java.lang.IllegalAccessException: access to class not allowed

ありがとうございました

4

2 に答える 2

6

オフィスを出ようとしているところなので、誰かがより詳細な回答を追加できることは間違いありませんが、AlarmManagerを使用する必要があります

Alarm Manager は、アプリケーションが現在実行されていない場合でも、特定の時間にアプリケーション コードを実行したい場合を対象としています。

于 2012-10-02T14:27:59.367 に答える
6

@Paul D'Ambraが言ったように、AlarmManagerでそれを行うことができます。

例:

まず、アラームを設定する必要があります。

AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);

Date futureDate = new Date(new Date().getTime() + 86400000);


futureDate.setHours(8);
futureDate.setMinutes(0);
futureDate.setSeconds(0);


Intent intent = new Intent(con, MyAppReciever.class);

PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);

次に、実行するコードを含むレシーバーを作成する必要があります (つまり、アプリを開始します)。

public class MyAppReciever extends BroadcastReceiver {

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

    startActivity(new Intent(context, MyApp.class));
    }
}
于 2012-10-02T14:31:09.870 に答える