0

I'm new to Android development. I am trying to monetize a live wallpaper that I built and the ad delivery company wants me to call their code from the onCreate of an activity.

The live wallpaper didn't have an activity before I started to monetize it, being an extension to WallpaperService, so I've added one. I've managed to create the activity and make it translucent, but it doesn't close when the dialog closes. I cannot edit the dialog code since it is being created by a call into a .jar, so I thought I could setup a listener for when the dialog is dismissed, but I wasn't able to find any practical examples that might help with the code below.

LWP.java

public class SBLiveWallpaper extends WallpaperService {
    super.onCreate();
    Intent i = new Intent();
  //  i.setClass(this, MainActivity.class);
    i.setComponent(new ComponentName("appname", "appname.MainActivity")); 
  //  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
}

activity_main.xml has no elements (just the RelativeLayout)

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppBucksAPI.initialize(this, APPID, "APIKEY", true, null, null);
    AppBucksAPI.userOptOutDialog(this, "marketname");
}

I could make the activity be non-transparent, and just add a close button, but that is ugly and confuses users.

Edit for clarification: I had tried originally to call the dialog directly from the service's onCreate(). It causes the LWP to crash in the screen where you can make it the active LWP. The error I get is android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application.

I contacted AppBucks support before making the original post here. Their response (pasted below) prompted me to create the translucent activity.:

I believe this error means that there is a problem with the first parameter you are passing to the AppBucksAPI.userOptOutDialog method… the call which looks like this from the docs:

AppBucksAPI.userOptOutDialog(this, "<App Name>");

This call expects an Activity or Activity context as the first parameter. It needs this because our default opt out dialog uses an AlertDialog call, which requires an active Activity for it to display correctly. If you are already creating an Activity along with your service, you should pass that activity as the first parameter instead of “this” (or you could move this call to the onCreate of that activity instead of onCreate for the service).

If you don’t have an Activity in your app, I found this StackOverflow question which has an answer that may help (in a nutshell, you can create a transparent activity when your service starts up, and make the userOptOutDialog call from that instead of your service’s onCreate method): Display AlertDialog as system overlay window from Service

Unfortunately, the above article covers creating the activity and closing the dialog under the assumption that the person reading it has access to the dialog's code. Since I do not have access to that, because it is imported into my project as a library, I need to know how to listen, from the parent activity, for the child to finish.

I did some digging and it looks like either of these could work, depending on how the activity is started from the dialog call my code makes:

http://developer.android.com/reference/android/app/Activity.html#finishActivityFromChild(android.app.Activity, int)

or

http://developer.android.com/reference/android/app/Activity.html#finishFromChild(android.app.Activity)

I'll give those a try tonight.

Community
  • 1
  • 1

3 Answers3

0

Looking at the AppBucks API and documentation, I don't think using an Activity is mandatory. It is just the most common way.

I think you can call AppBucks method in your service onCreate as well?

nicopico
  • 3,606
  • 1
  • 28
  • 30
  • Hi, thanks. I've updated my original question. I had originally tried that but it causes the LWP to crash with error: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application. – user2037908 Feb 07 '13 at 22:14
0

When dismissing your dialog, send an intent to your activity for it to close itself.

For instance

Put this in the dialog dismiss method:

sendBroadcast(new Intent(MainActivity.ACTION_TERMINATE));

Then in the MainActivity add and register a BroadcastReceiver:

Add fields for the receiver and the filter in the activity:

private ActivityBroadcastReceiver mReceiver;
static final IntentFilter mFilter = new IntentFilter();
static {mFilter.addAction(ACTION_TERMINATE);}

Instantiate it in onCreate():

mReceiver = new ActivityBroadcastReceiver();

Register it in onResume():

    registerReceiver(mReceiver, mFilter);

Unregister it in onPause():

unregisterReceiver(mReceiver);

And the broadcast receiver's inner class in the activity would look like this

private class ActivityBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String action = intent.getAction();
            if (ACTION_TERMINATE.equals(action)) {
                finish();
            }
        } catch (Exception e) {
            Log.w(mTag, "Oops: " + e, e);
        }
    }
}
gpo
  • 3,388
  • 3
  • 31
  • 53
  • As I mentioned, I don't have access to the dialog code, so that isn't possible. Is there any way to make a listener or broadcast receiver that might work on a more global level, possibly by getting a handle to the dialog's activity itself? If I could listen for that activity being destroyed, from within the parent activity (the transparent one that I created), then I could just call finish() within my own activity when I see that the child was destroyed. – user2037908 Feb 07 '13 at 21:40
0

The AppBucks SDK also exposes the following functions:

setIconAdsEnabledForUser
setPushAdsEnabledForUser

The AppBucksAPI.userOptOutDialog is basically a convenience function that wraps calls to these in an AlertDialog. For your app, it probably makes more sense to forego the convenience function and write your own AlertDialog that calls the enable functions directly. That way you will have full control over what happens when the dialog is dismissed and can close the new activity you created when you need to.

Rudism
  • 1,585
  • 10
  • 13