简体   繁体   English

在 Android 10 中从服务启动 Activity

[英]Start Activity from Service in Android 10

please note that problem might be related to Android 10请注意,问题可能与 Android 10 有关

Im trying to start a new Activity from myInAppMessagingService, but i got null pointer exception un startActivitys context parameter every time.我试图从 myInAppMessagingService 启动一个新活动,但我每次都收到空指针异常 un startActivitys 上下文参数。

So here is my Service code :所以这是我的服务代码:

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.firebase.inappmessaging.FirebaseInAppMessagingClickListener;
import com.google.firebase.inappmessaging.model.Action;
import com.google.firebase.inappmessaging.model.CampaignMetadata;
import com.google.firebase.inappmessaging.model.InAppMessage;

import viaapp_v2.systems.webview_activity.webview_base;

public class MyFirebaseInAppMessaging extends Service implements FirebaseInAppMessagingClickListener {

    String TAG = "MyFirebaseInAppMessaging";

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void messageClicked(@NonNull InAppMessage inAppMessage, @NonNull Action action) {
        // Determine which URL the user clicked
        String url = action.getActionUrl();
        Log.d(TAG, "Popup URL :"+url);

        // Get general information about the campaign
        CampaignMetadata metadata = inAppMessage.getCampaignMetadata();
        Log.d(TAG, "metadata :"+metadata);

        try{
            startActivity(
                    new Intent(MyFirebaseInAppMessaging.this, webview_base.class)
                            .putExtra("web_url", url)
            );
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

I got error in "startActivity(..)" line because of 1st parameter, context.由于第一个参数上下文,我在“startActivity(..)”行中出错。 I tried everything - getApplicationContext(), MyFirebaseInAppMessaging.this or just simply "this" but nothing works.我尝试了所有方法 - getApplicationContext()、MyFirebaseInAppMessaging.this 或只是简单的“this”,但没有任何效果。

I read a restrictions provided by Android Developers, but i couldnt figure out anything new.我阅读了 Android Developers 提供的限制,但我想不出任何新的东西。

Otherwise the app works perfectly - webview_base class works as it should, so does everything else, including myInAppMesaging Services listener.否则,该应用程序可以完美运行 - webview_base 类可以正常工作,其他所有内容也是如此,包括 myInAppMesaging 服务侦听器。 Its just that one context in startActivity() which stops me.它只是 startActivity() 中的一个上下文,它阻止了我。

Thanks for any help.谢谢你的帮助。

--Update on Sep 7 -- 9 月 7 日更新

After playing around with permissions, flags ect.在玩弄权限后,标志等。 i noticed that nothing works.我注意到没有任何效果。 Newer Android OS opens an web overly over the app, but older Android OS just crashes without any specific crash report.较新的 Android 操作系统会在应用程序上过度打开网络,但较旧的 Android 操作系统只会在没有任何特定崩溃报告的情况下崩溃。 Thats weird.这很奇怪。

Try below code , it will work..试试下面的代码,它会工作..

you have to add FLAG- FLAG_ACTIVITY_NEW_TASK你必须添加 FLAG- FLAG_ACTIVITY_NEW_TASK

Intent myIntent = new Intent(this, webview_base.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);

If your app is not visible and has not been visible for a while and you don't want to use a notification, it will not be allowed in Android 10, except if如果您的应用不可见且有一段时间不可见,并且您不想使用通知,则 Android 10 中将不允许使用通知,除非

The app has been granted the SYSTEM_ALERT_WINDOW permission by the user.该应用已被用户授予SYSTEM_ALERT_WINDOW权限。

(From the restrictions page mentioned in the question) (来自问题中提到的限制页面)

So that's an option for Android 10.所以这是 Android 10 的一个选项。

It's not enough to list the SYSTEM_ALERT_WINDOW in AndroidManifest.xml.在 AndroidManifest.xml 中列出SYSTEM_ALERT_WINDOW是不够的。 You also need user to give the app "Draw over other apps" permission.您还需要用户授予应用程序“绘制其他应用程序”权限。 Check here how to do that easily . 在这里查看如何轻松做到这一点

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

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