2012-12-12 48 views
1

我研究了自己的應用程序的alarmmanager和autostart。我想有一個無形的服務(這是不正確的名字,對不起),顯示我週期性的敬酒(爲了測試目的)。定期執行警報(自動啓動)

以下代碼運行但不正確。我點擊 「按鈕」,活動消失,但沒有...

我的代碼:

MainActivity 

public class MainActivity extends Activity { 

/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button buttonStart = (Button)findViewById(R.id.button1); 
     buttonStart.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
    Intent myIntent = new Intent(getBaseContext(), 
     MyScheduledReceiver.class); 

    PendingIntent pendingIntent 
    = PendingIntent.getBroadcast(getBaseContext(), 
     0, myIntent, 0); 

    AlarmManager alarmManager 
     = (AlarmManager)getSystemService(ALARM_SERVICE); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.add(Calendar.SECOND, 10); 
    long interval = 60 * 10; // 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
     calendar.getTimeInMillis(), interval, pendingIntent); 
    finish(); 
    }}); 
    } 

} 


AutoStartNotifyReceiver 

public class AutoStartNotifyReceiver extends BroadcastReceiver { 
    private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED"; 
    public void onReceive(Context context, Intent intent) { 
     if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){ 

      Intent myIntent = new Intent(context, MyScheduledReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,  myIntent, 0); 

      AlarmManager alarmManager =   (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.add(Calendar.SECOND, 10); 
      long interval = 60 * 10; 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,   calendar.getTimeInMillis(), interval, pendingIntent);   
     } 
    } 
} 

MyScheduledActivity 

public class MyScheduledActivity extends Activity { 

MediaPlayer player; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    //Toast.makeText(getApplicationContext(), "Eseguo onCreate",  Toast.LENGTH_LONG).show(); 
    setContentView(R.layout.activity_main); 

    player = MediaPlayer.create(this, R.raw.gabriel); 
    player.start(); 



    Toast.makeText(this, "miao",Toast.LENGTH_LONG).show(); 

} 

} 

MyScheduledReceiver 

public class MyScheduledReceiver extends BroadcastReceiver { 

public void onReceive(Context context, Intent intent) { 

Intent scheduledIntent = new Intent(context, MyScheduledActivity.class); 
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(scheduledIntent); 
} 
} 

清單:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
<receiver android:name=".AutoStartNotifyReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    </intent-filter> 
</receiver> 

{12-12 14:50:21.411:E/AndroidRuntime( 865):致命例外:main 12-12 14:50:21.411:E/AndroidRuntime(865):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.servi/com.example.servi.M​​yScheduledActivity} :java.lang.NullPointerException 12-12 14:50:21.411:E/AndroidRuntime(865):at androi d.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 12-12 14:50:21.411:E/AndroidRuntime(865):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 12-12 14:50:21.411:E/AndroidRuntime(865):在android.app.ActivityThread.access $ 600(ActivityThread.java:123) 12-12 14:50:21.411:E/AndroidRuntime(865):at android.app .ActivityThread $ H.handleMessage(ActivityThread.java:1147) 12-12 14:50:21.411:E/AndroidRuntime(865):at android.os.Handler.dispatchMessage(Handler.java:99) 12-12 14 :50:21.411:E/AndroidRuntime(865):在android.os.Looper.loop(Looper.java:137) 12-12 14:50:21.411:E/AndroidRuntime(865):at android.app.ActivityThread .main(ActivityThread.java:4424) 12-12 14:50:21.411:E/AndroidRuntime(865):at java.lang.reflect.Method.invo keNative(Native Method) 12-12 14:50:21.411:E/AndroidRuntime(865):at java.lang.reflect.Method.invoke(Method.java:511) 12-12 14:50:21.411:E/AndroidRuntime(865):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 12-12 14:50:21.411:E/AndroidRuntime(865):at com.android.internal .os.ZygoteInit.main(ZygoteInit.java:551) 12-12 14:50:21.411:E/AndroidRuntime(865):at dalvik.system.NativeStart.main(Native Method) 12-12 14:50: 21.411:E/AndroidRuntime(865):引起:java.lang.NullPointerException 12-12 14:50:21.411:E/AndroidRuntime(865):at com.example.servi.M​​yScheduledActivity.onCreate(MyScheduledActivity.java:22 ) 12-12 14:50:21.411:E/AndroidRuntime(865):at android.app.Activity.performCreate(Activity.java:4465)在Android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-12 14:50:21.411:E/AndroidRuntime(865):at:12-12 14:50:21.411:E/AndroidRuntime(865) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 12-12 14:50:21.411:E/AndroidRuntime(865):... 11更 }

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

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

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

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.servi.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

<receiver android:name=".AutoStartNotifyReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    </intent-filter> 
</receiver> 
<receiver android:name="MyScheduledReceiver"></receiver> 
<activity android:name="MyScheduledActivity"></activity> 
</application> 
</manifest> 

回答

1

http://www.androidsnippets.com/autostart-an-application-at-bootup

(應用部分):

<receiver android:enabled="true" android:name=".BootUpReceiver" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 

     <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
</receiver> 
[..] 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
[..] 

public class BootUpReceiver extends BroadcastReceiver{ 

     @Override 
     public void onReceive(Context context, Intent intent) { 
       Intent i = new Intent(context, MyActivity.class); 
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(i); 
     } 

} 

您可以使用定時器類使用TimerTask安排週期性任務。你也可以使用ScheduledThreadPoolExecutor類。

AlarmManager可以是一個很好的選擇。警報管理器適用於您希望在特定時間運行應用程序代碼的情況,即使您的應用程序當前未運行。

+0

只有現在我有時間測試代碼。完美運行!謝謝!我的目標是每天2次(上午10點和晚上10點)創建通知(如敬酒)。這是使用警報管理器的最佳方式嗎?你有這段代碼的片段嗎?謝謝! –

+0

@PolHallen,它可能會幫助你 - > http://stackoverflow.com/questions/4252907/how-to-set-a-persistent-regular-schedule-in-android –

0

清單中添加一個接收器...爲您的警報收到。 就你的情況而言;

<receiver android:name=".MyScheduledReceiver"> 

</receiver> 

我希望它會解決這個問題

PS:您的活動MyScheduledActivity將被重複兩次,在第二幾乎每600毫秒後開始....所以不要做...增加間隔:) 或做別的檢查可能打印日誌或東西在AndroidManifest.xml測試

+0

的感謝!我更新了我的代碼和logcat。我有nullpointerexception。也感謝600毫秒(僅用於測試)。 –