简体   繁体   English

通知启动应用程序将活动带到前面

[英]Notification start application else bring activity to front

I have multiple activities in our application. 我的申请中有多项活动。 One of the activities are starting up activity which is main launcher activity. 其中一项活动是启动活动,这是主要的发射器活动。 It starts and initaties few classes which are used by whole application and finishs. 它启动并初始化几个类,这些类被整个应用程序使用并完成。

Then there is main activity where user resides most of their time. 然后是主要活动,用户大部分时间都在这里活动。 When notification is created if the application is closed then those classes are destroyed and therefore application gives error (nullpointerexception). 如果应用程序关闭时创建通知,则会销毁这些类,因此应用程序会出错(nullpointerexception)。

What we need to do is start the launcher activity if application is closed else bring the main activity to front. 我们需要做的是在应用程序关闭时启动启动器活动,否则将主要活动放在前面。

I have tried mutliple solutions here. 我在这里试过多种解决方案。

None of these solutions works. 这些解决方案都不起作用。 Any help is appreciated. 任何帮助表示赞赏。

Update 1: 更新1:

I though what if we choose which intent should pending intent get by checking if the app is running or not. 我虽然如果我们通过检查应用程序是否正在运行来选择应该通过挂起的意图获得哪个意图。

public boolean isApplicationRunning(Context context) {
    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (RunningTaskInfo task : tasks) {
        if (context.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
            return true;
    }

    return false;
}

This fixes the problem we have with multiple activities. 这解决了我们在多个活动中遇到的问题。 But this gives another problem, what if user is in main activity and notification is received then user closes application but doesn´t click on notification. 但这会产生另一个问题,如果用户处于主要活动并收到通知,那么用户关闭应用程序但不点击通知。 Later when user will click on notification application will again give nullpointerexception because at the time of notification was created application was running. 稍后当用户点击通知时,应用程序将再次给出nullpointerexception,因为在创建通知时,应用程序正在运行。

There must be a better way to handle this problem. 必须有更好的方法来处理这个问题。

No one is being able to answer my question. 没有人能够回答我的问题。 So, I was forced to use a solution which is little dirty. 所以,我被迫使用一点点脏的解决方案。

So we will need one more class NotificationActivity . 所以我们还需要一个类NotificationActivity

Wheneven a new notification is received from back-end server. 从后端服务器收到新通知时。 We will send pendingIntent to NotificationActivity. 我们将pendingIntent发送到NotificationActivity。

NotificationCompt.Builder = bla bla bla.
Intent intent = new Intent(context, NotificationActivity.class);
intent.setFlags(FLAG_ACTIVITY_CREATE_NEW_TASK);

PendingIntent pIntent = PendingIntent.startIntent(0, intent, 0, someflags);

In NotificationActivity: 在NotificationActivity中:

public class NotificationActivity extends Activity {

     @Override
     protected void onCreate(Bundle savedInstances) {
         Intent intent = null;

         try {
             // Call any method from class which is need by Main activity
             someclass.somemethod();

             // If we are here then it means app is running.
             intent = new Intent(context, MainActivity.class);

             // Flag it to bring it to front
             intent.setFlags(FLAG_ACTIVITY_BRING_TO_FRONT);
         } catch (NullPointerException e ) {

             // If we are here then it means that app is closed and 
             // someclass is garbage collected.
             intent = new Intent(context, StartAppActivity.class);

             // Flag it to create new task
             intent.setFlags(FLAG_ACTIVITY_CREATE_NEW_TASK);
         }

         // Start intent, and finish this activity
         startActivity(intent);
         finish();
     }
}

This solves all the problems that i have mentioned above. 这解决了我上面提到的所有问题。 Don´t know if it creates new problems. 不知道它是否会产生新问题。 If you use this then use a method which has low-cost. 如果您使用此方法,请使用低成本的方法。 This activity will be garbage collected as soon as u finish it. 完成后,此活动将被垃圾收集。 Ok! 好! I lie, in the nearest future. 我在最近的将来撒谎。

This would have given problem if u had multiple notifications lying, but android lets you put all notifications into a single pendingIntent so the problem will never arise. 如果您有多个通知,这会产生问题,但是android允许您将所有通知放入单个pendingIntent中,因此问题永远不会出现。

I cannot come up with any other problems .. if u can then tell me or wait until it is tested :D 我不能提出任何其他问题..如果你可以告诉我或等到它被测试:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM