我想用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>
有誰知道哪裏有問題?我還需要這個新的活動解鎖屏幕,如果它被鎖定,就像鬧鐘或通話一樣。
您可以使用等待指定秒數的線程從'main.java'開始新的活動,然後用'startActivity'開始一個活動。我不明白使用廣播接收機的意義嗎? –
這是因爲即使應用程序在此期間關閉,也需要啓動新的活動。 – user3051755
@Sher Alam - 不,那會是錯誤的做法。這樣您就可以在UI線程中等待並阻止與應用程序進行交互的任何方式。 – shadox