使用BroadcastReceiver在几秒钟后启动新活动

时间:2016-03-28 11:51:52

标签: java android

我想在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>

有谁知道问题出在哪里?我还需要这个新的活动解锁屏幕,如果它被锁定,如报警或通话。

1 个答案:

答案 0 :(得分: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>