【问题标题】:Bring the app to the foreground after event on react native在本机反应事件发生后将应用程序置于前台
【发布时间】:2022-11-14 04:25:50
【问题描述】:

当我的 socket-io 收到事件时,在用户点击主页按钮后,我试图将我的应用程序置于前台,在我的 Javascript 组件和我的 java 模块之间创建一个桥梁后,该功能工作正常,但我的应用程序没有出现在前

这是我的活动(这个活动也很好):

const onEvent = () => {
   newRideCall.bringTheApp();
}

这是我的模块

package com.taxitecdriverapp;

import android.content.Intent;
import android.widget.Toast;
import android.content.Context;
import android.app.ActivityManager;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import javax.annotation.Nonnull;

public class NewRideCallModule extends ReactContextBaseJavaModule {

    public static final String REACT_CLASS = "NewRideCall";
    private static ReactApplicationContext reactContext;

    public NewRideCallModule(@Nonnull ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Nonnull
    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @ReactMethod
    public void bringTheApp() {

        //I have ben try this first, not workin
        Intent i = new Intent(reactContext, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_NEW_TASK);
        reactContext.startActivity(i);

         // and now this, both of them not working
         Context context = getReactApplicationContext();
         String pn = context.getApplicationContext().getPackageName();
         Intent li = context.getPackageManager().getLaunchIntentForPackage(pn);
         context.startActivity(li);

         // this Toast shows up fine
         Toast.makeText(reactContext, "TAXITEC wakeLockPhone called 0", Toast.LENGTH_LONG).show();
    }
}

【问题讨论】:

    标签: javascript java android react-native


    【解决方案1】:

    检查以下代码。这对我很有效

      private final ReactApplicationContext reactContext;
      public LibraryModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
      }
    
      @ReactMethod
      public void resurrectApp(){
        Intent launchIntent = reactContext.getPackageManager().getLaunchIntentForPackage(reactContext.getPackageName());
        if (launchIntent != null) {
          reactContext.startActivity(launchIntent);
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 2020-06-20
      相关资源
      最近更新 更多