简体   繁体   中英

Start Activity from Service in Android 10

please note that problem might be related to Android 10

Im trying to start a new Activity from myInAppMessagingService, but i got null pointer exception un startActivitys context parameter every time.

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. I tried everything - getApplicationContext(), MyFirebaseInAppMessaging.this or just simply "this" but nothing works.

I read a restrictions provided by Android Developers, but i couldnt figure out anything new.

Otherwise the app works perfectly - webview_base class works as it should, so does everything else, including myInAppMesaging Services listener. Its just that one context in startActivity() which stops me.

Thanks for any help.

--Update on Sep 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. Thats weird.

Try below code , it will work..

you have to add 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

The app has been granted the SYSTEM_ALERT_WINDOW permission by the user.

(From the restrictions page mentioned in the question)

So that's an option for Android 10.

It's not enough to list the SYSTEM_ALERT_WINDOW in AndroidManifest.xml. You also need user to give the app "Draw over other apps" permission. Check here how to do that easily .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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