HOWTO编程"启动" Android应用程序? [英] howto programatically "restart" android app?

查看:107
本文介绍了HOWTO编程"启动" Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道不应该真的杀/重新启动的Andr​​oid应用程序。在我的使用情况下,我想出厂重置我的应用程序在服务器向客户端发送一个特定信息的具体情况。用户只能记录在服务器上的应用程序的一个实例(即多个设备是不允许的)。如果另一个实例获取已登录-lock则该用户的所有其他实例必须删除他们的数据(出厂重置),以保持一致性。能够强制地获得锁,因为用户可能会删除该应用和重新安装,这将导致在不同的实例的编号和用户将不能够再释放该锁。因此,能够强制地获得锁。

Firstly, I know that one should not really kill/restart an Application in Android. In my use case I want to factory-reset my application in a specific case where a server sends a specific information to the client. The user can only be logged in on the server with ONE instance of the application (i.e. multiple devices are not allowed). If another instance gets that "logged-in"-lock then all other instances of that user have to delete their data (factory-reset), to maintain consistency. It is possible to forcibly get the lock, because the user might delete the app and reinstall it which would result in a different instance-id and the user would not be able to free the lock anymore. Therefore it is possible to forcibly get the lock.

我们需要经常检查在一个具体的实例,它有锁。做到这一点上(几乎)每个请求到服务器。服务器可能会发出一种错锁ID。如果被检测到,则客户端应用必须删除一切

Because of that force-possibility we need to always check in an concrete instance that it has the lock. That is done on (almost) each request to the server. The server might send a "wrong-lock-id". If that is detected, the client application must delete everything.

这是用例。没有了实现的问题:

That was the use-case. No for the implementation-question:

我有一个活动 A启动登录活动 L或应用程序的主活动 B依赖于共享preFS领域。起始L或乙之后它,以便仅L或B正在运行关闭本身。 因此,在该用户已登录已经乙现在正在运行的情况下

I have an Activity A that starts the Login Activity L or the app's main Activity B depending on a sharedPrefs field. After starting L or B it closes itself so that only L or B is running. So in the case that the user is logged in already B is running now.

B开始C.的C调用 startService IntentService D。这会导致栈:

B starts C. C calls startService for the IntentService D. That results in this stack:

(A)> B> C> D

(A) > B > C > D

从D的onHandleIntent方法的事件发送到 ResultReceiver R上。

from the onHandleIntent method of D an event is send to a ResultReceiver R.

研究,现在通过提供用户一个对话框,在那里他可以选择处理该事件出厂重置应用程序(删除数据库,共享preFS等)

R now handles that event by providing the user a dialog where he can choose to factory-reset the application (delete db, sharedPrefs etc.)

出厂重置我要重新启动应用程序(关闭所有的活动),并只启动一次A,然后启动登录活动 L和完成自身后:

After the factory-reset I want to restart the application (to close all activities) and only start A again which then launches the Login Activity L and finishes itself:

(A)>→

在对话框的onClick法看起来是这样的:

The Dialog's onClick-method looks like this:

@Override
public void onClick(DialogInterface dialog, int which) {
    // will call onCancelListener
    MyApplication.factoryReset(); // (deletes db, clears sharedPrefs etc.)
    Intent i = new Intent(MyApp.getContext(), A.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    MyApp.getContext().startActivity(i);
}

和多数民众赞成在的MyApp 类:

public class MyApp extends Application {
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

    public static Context getContext() {
        return context;
    }

    public static void factoryReset() {
        // ...
    }
}

现在的问题是,如果我使用 FLAG_ACTIVITY_NEW_TASK 的活动B和C仍在运行。如果我打BTN在登录活动我看到C上回来,但我想回到主屏幕。 如果我没有设置 FLAG_ACTIVITY_NEW_TASK 我得到的错误:

The Problem now is that if I use the FLAG_ACTIVITY_NEW_TASK the Activities B and C are still running. If I hit the back btn on the Login Activity I see C, but I want to go back to the Home screen. If I do not set the FLAG_ACTIVITY_NEW_TASK I get the error:

07-07 12:27:12.272: ERROR/AndroidRuntime(9512): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

我不能使用活动上下文,因为 ServiceIntent D也可能从后台任务叫这由开始的 AlarmManager

I cannot use the Activities' Context, because the ServiceIntent D might also be called from an background task which is started by the AlarmManager.

所以,我怎么能解决这个问题的活动栈变成:(A)>→

So how could I solve this to the activity stack becoming: (A) > L ?

推荐答案

您可以使用<一个href="http://developer.android.com/reference/android/app/PendingIntent.html"><$c$c>PendingIntent设置启动你的启动活动,并在未来再关闭应用程序

You can use PendingIntent to setup launching your start activity in the future and then close your application

Intent mStartActivity = new Intent(context, StartActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);

这篇关于HOWTO编程&QUOT;启动&QUOT; Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆