2016-03-28 116 views
0

我想用BroadcastReceiver在幾秒鐘後開始新的活動。即使應用程序在此期間關閉,也需要啓動新的活動。用BroadcastReceiver在幾秒鐘後開始新的活動

現在我有這個,但是當我運行它沒有任何反應:

Main.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button button = (Button) findViewById(R.id.button); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getApplicationContext(), Receiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0); 
      int seconds = 5; 
      Calendar calendar = Calendar.getInstance(); 
      calendar.add(Calendar.SECOND, seconds); 
      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
      alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);; 
     } 
    }); 
} 

Receiver.java

public class Receiver extends BroadcastReceiver { 
    private PowerManager.WakeLock mWakelock; 
    @SuppressWarnings("deprecation") 
    private KeyguardManager.KeyguardLock mLock; 
    private static ContentResolver sResolver; 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onReceive(Context paramContext, Intent intent) { 

     this.mWakelock = ((PowerManager) paramContext.getSystemService("power")) 
       .newWakeLock(805306394/* | PowerManager.ON_AFTER_RELEASE */, 
         "wakelock"); 
     this.mWakelock.acquire(); 
     this.mLock = ((KeyguardManager) paramContext 
       .getSystemService("keyguard")).newKeyguardLock(""); 
     this.mLock.disableKeyguard(); 

     setLockPatternEnabled(true); 

     sResolver = paramContext.getContentResolver(); 

     Intent startMain; 
     startMain = new Intent(paramContext, NewActivity.class); 
     startMain.setAction("com.name.NewActivity"); 
     startMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     paramContext.startActivity(startMain); 
    } 

    /** 
    * used for to enable lock in all patterns 
    * 
    * @param enabled 
    */ 
    @SuppressWarnings("deprecation") 
    public static void setLockPatternEnabled(boolean enabled) { 
     setBoolean(android.provider.Settings.System.LOCK_PATTERN_ENABLED, 
       enabled); 
    } 

    private static void setBoolean(String systemSettingKey, boolean enabled) { 
     android.provider.Settings.System.putInt(sResolver, systemSettingKey, 
       enabled ? 1 : 0); 
    } 
} 

AndroidManifest.xml中

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Light.NoTitleBar" > 
     <activity 
      android:name=".Main" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

     <activity 
      android:name="com.name.NewActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.name.NewActivity" /> 

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

</manifest> 

有誰知道哪裏有問題?我還需要這個新的活動解鎖屏幕,如果它被鎖定,就像鬧鐘或通話一樣。

+0

您可以使用等待指定秒數的線程從'main.java'開始新的活動,然後用'startActivity'開始一個活動。我不明白使用廣播接收機的意義嗎? –

+0

這是因爲即使應用程序在此期間關閉,也需要啓動新的活動。 – user3051755

+0

@Sher Alam - 不,那會是錯誤的做法。這樣您就可以在UI線程中等待並阻止與應用程序進行交互的任何方式。 – shadox

回答

1

你可以做這樣的事情:(忽略這一項)

int millis = 500; 
new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     startActivity(new Intent(SplashScreenActivity.this, HomeActivity.class)); 
      SplashScreenActivity.this.finish(); 
     } 
    }, millis); 

更新1(應該工作):試試這個在您的MainActivity,你在錯誤的方式可能計數時間,所以使用日曆。如果有效,我會盡力解釋。

Intent intent = new Intent(this, Receiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0); //Or you can get broadcast in your way. 
int seconds = 25; 
Calendar calendar = Calendar.getInstance(); 
calendar.add(Calendar.SECOND, seconds); 
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

更新2:你應該在清單註冊您的接收器。在某些情況下,接收器上有意圖過濾器,但我應該這樣工作。確保包裝和接收器的名稱是正確的。

<application ...> 
    <activity ...> 
    ... 
    <receiver android:name=".package.YourReceiver"> 

    </receiver> 
    ... 
</application> 
+0

即使應用在此期間關閉,我也需要啓動新的活動。 – user3051755

+0

我試過更新1,但它不起作用,當我運行它時沒有任何反應。 – user3051755

+0

你能否添加你的清單文件併爲Main.java添加缺少的代碼?你在哪裏設置報警管理器?在onCreate?你能添加更多的代碼嗎? – shadox