2012-10-02 22 views
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);}} 

であり、これは私が今

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

私の放送トリガをマニフェストにこの行を追加したreciverクラスコード

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}} 

ですが、私はこのエラーを得た

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 

ので間違いなく誰かが、より詳細な回答を追加することはできませんあなたのオフィスの外に歩いてちょうど約

+0

私の例を見てください。 –

+0

次の日付で試すfutureDate = new Date(new Date()。getTime()); Dateのnstead futureDate = new Date(新しいDate()。getTime()+ 86400000); –

+0

私はfuture..Dateの代わりにコードを変更しました.1分間でトリガーブロードキャスト用のカレンダーオブジェクトを使用しています.1分後に強制終了ダイアログが表示されました。 "IllegalAccessException:クラスへのアクセスが許可されていません" –

答えて

7

@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); 

あなたは(あなたのアプリを起動するIE-)を実行するためにいくつかのコードとのレシーバを作成する必要があります次:

public class MyAppReciever extends BroadcastReceiver { 

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

    startActivity(new Intent(context, MyApp.class)); 
    } 
} 
+2

このコード私はちょうど編集テキストからの入力を取ってそれを futureDate.setHours(i)に挿入する必要があります。 futureDate.setMinutes(j);右? –

+0

編集テキストを使用して起床日を設定している場合は、時間、分、または秒を設定するだけで済みます。あなたの例では –

+0

"con"オブジェクトは何ですか? con.getSystemService() –

6

アムありがとうございましたが、あなたはアラームマネージャは、あなたがしたい場合のために意図されてAlarmManager

を使用する必要があります が現在実行されていない場合でも、特定の時刻に アプリケーションコードを実行してください。

関連する問題