Android BroadcastReceiver 并启动意图

发布于 2024-12-12 02:14:28 字数 1620 浏览 0 评论 0原文

public class SessionManager extends BroadcastReceiver{
Date timeOff;
Date timeOn;

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


    if( "android.intent.action.SCREEN_OFF".equals(intent.getAction())) {
        Log.i("MobileViaNetReceiver", "Screen off - start time to end session");
        timeOff = Calendar.getInstance().getTime();
    } 

    if( "android.intent.action.ACTION_SHUTDOWN".equals(intent.getAction())) {
        // DO WHATEVER YOU NEED TO DO HERE
        Log.i("MobileViaNetReceiver", "Shut down - log off user");
        DbAdapter_User db = new DbAdapter_User(context);
        db.open();
        db.handleLogout();
        db.close();
    } 

    if( "android.intent.action.SCREEN_ON".equals(intent.getAction())) {

        timeOn = Calendar.getInstance().getTime();

        long diffInMs = timeOn.getTime()-timeOff.getTime();

        // convert it to Minutes
        long diffInMins = TimeUnit.MILLISECONDS.toMinutes(diffInMs);

        if ((int) (diffInMins) > 15) {
            //log out user
            Log.i("MobileViaNetReceiver", "User inactive for 15 minutes - logout user");
            DbAdapter_User db = new DbAdapter_User(context);
            db.open(); // ******* HERE *************
            db.handleLogout();
            db.close();

        } else {
            Log.i("MobileViaNetReceiver", "User still active");
        }
    }   
}

当屏幕打开时,我检查用户是否关闭屏幕超过 15 分钟,如果是,则注销他。然后转到 LonIn 屏幕。 我想在调用handleLogout()时启动一个意图(标记为*此处**) 当类扩展 BroadcastReceiver 时我可以这样做吗?如果没有,我还能做什么?

public class SessionManager extends BroadcastReceiver{
Date timeOff;
Date timeOn;

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


    if( "android.intent.action.SCREEN_OFF".equals(intent.getAction())) {
        Log.i("MobileViaNetReceiver", "Screen off - start time to end session");
        timeOff = Calendar.getInstance().getTime();
    } 

    if( "android.intent.action.ACTION_SHUTDOWN".equals(intent.getAction())) {
        // DO WHATEVER YOU NEED TO DO HERE
        Log.i("MobileViaNetReceiver", "Shut down - log off user");
        DbAdapter_User db = new DbAdapter_User(context);
        db.open();
        db.handleLogout();
        db.close();
    } 

    if( "android.intent.action.SCREEN_ON".equals(intent.getAction())) {

        timeOn = Calendar.getInstance().getTime();

        long diffInMs = timeOn.getTime()-timeOff.getTime();

        // convert it to Minutes
        long diffInMins = TimeUnit.MILLISECONDS.toMinutes(diffInMs);

        if ((int) (diffInMins) > 15) {
            //log out user
            Log.i("MobileViaNetReceiver", "User inactive for 15 minutes - logout user");
            DbAdapter_User db = new DbAdapter_User(context);
            db.open(); // ******* HERE *************
            db.handleLogout();
            db.close();

        } else {
            Log.i("MobileViaNetReceiver", "User still active");
        }
    }   
}

When the screen is turned ON I am checking if the user has turned scrren off for more than 15 mins, if yes, logout him. And go to LonIn screen.
I want to start an intent when I call that handleLogout() (marked * HERE **)
Can I do that when class extends BroadcastReceiver ? If no, what else can I do ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静 2024-12-19 02:14:28

你需要记住intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

向编写以下错误消息的 Google 工程师致以祝福和幸福:“E/AndroidRuntime(2339): java.lang.RuntimeException: 无法启动接收器 android.util.AndroidRuntimeException: 从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK这真的是你想要的吗?”

public class AgeingAutoStartBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Intent intent1 = new Intent(context,MyMainClass.class);
    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent1);
    );  
}

感谢所有

在这里写下有用信息的人

You need to remember intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Blessings and happiness to the Google engineer who wrote the followint error message "E/AndroidRuntime( 2339): java.lang.RuntimeException: Unable to start receiver android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?"

public class AgeingAutoStartBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Intent intent1 = new Intent(context,MyMainClass.class);
    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent1);
    );  
}

}

Thanks to everyone who writes helpful messages here

無心 2024-12-19 02:14:28

是的,你可以这样做。您只需使用创建 Intent 时传递到 onRecieve 函数中的上下文即可。一旦你有了意图,就拨打这个电话:

Context.startActivity(yourIntent);

Yes, you can do that. You just use the context that was passed into your onRecieve function when you're creating your Intent. Once you have the Intent, make this call:

Context.startActivity(yourIntent);
Oo萌小芽oO 2024-12-19 02:14:28

你当然可以。尝试

Intent yourIntent = new Intent(context, YourActivity.class);
startActivity(yourIntent);

如果您想在 handleLogout 方法中执行此操作,请传入 Context。

private method handleLogout(Context context) {
    ...
}

You certainly can. Try

Intent yourIntent = new Intent(context, YourActivity.class);
startActivity(yourIntent);

If you want to do it in your handleLogout method, pass in the Context.

private method handleLogout(Context context) {
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文