Android - Getting data out of Startapp - java

I use Startapp as my ad network in my applications and I need to be able to handle when a user clicks on an ad. I could not find any documentation for this function, and I would rather use onPause() because other things might pause the activity. Help would be appreciated!

Try this:
YourStartAppAd.showAd(new AdDisplayListener() {
#Override
public void adHidden(Ad ad) {
}
#Override
public void adDisplayed(Ad ad) {
}
#Override
public void adClicked(Ad ad) {
}
});
https://github.com/StartApp-SDK/Documentation/wiki/android-advanced-usage

Related

How to implement Reward video with Startapp

Recently I'm facing issue related to Startapp platform for Android.
When I implemented the reward video it returns an error with:
FailledError execute Exception Error sendGetWithResponse code = [204]
Here is my function:
public void rewardAd() {
Log.i("TAG", "Test");
startAppAd.setVideoListener(new VideoListener() {
#Override
public void onVideoCompleted() {
Log.i("TAG", "Grant User");
}
});
startAppAd.loadAd(StartAppAd.AdMode.REWARDED_VIDEO, new AdEventListener() {
#Override
public void onReceiveAd(com.startapp.android.publish.adsCommon.Ad ad) {
startAppAd.showAd();
}
#Override
public void onFailedToReceiveAd(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "Failled"+ ad.getErrorMessage());
startAppAd.showAd(new AdDisplayListener() {
#Override
public void adHidden(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "adHidden");
}
#Override
public void adDisplayed(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "adDisplayed");
}
#Override
public void adClicked(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "adClicked");
}
#Override
public void adNotDisplayed(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "adNotDisplayed"+ ad.getErrorMessage());
}
});
//startAppAd.showAd(getApplicationContext());
}
});
}
Here is the logs that shows:
TAG: Test
TAG: FailledError execute Exception Error sendGetWithResponse code = [204]
TAG: adNotDisplayed
When I comment the function startAppAd.showAd(), and uncomment the last commented one startAppAd.showAd(), it's working fine.
This is the expected working scheme:
Try to load a Video Reward Ad => startAppAd.loadAd();
if failed (like my case) => onFailedToReceiveAd();
Try to show an Ad that I can get listeners of it => startAppAd.showAd()
Thank You very much
It's solved, the script itself is not bad and works fine, the problem was with Startapp they don't show a video ad because it doesn't exist any video to show.
Now if someone is facing the same problem i suggest to use a vpn on testing device with a country that startapp should have a video ad usually US.

How can i do something before app is closed? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have made an App in android in which i have a TCP Client but now i want to send a message to the server only when the app is going to be closed, i've trying to add the openConnection (opening connection with TCP Client) and sendMessage (Sending message to the TCP server) action in onDestroy method but that didn't worked.
The TCP Client i've used is in this guide, actually i need to send this message for communicate the server that the communication with the device is closed and send message "Device is Offline" and just then close the app.
Method 1: You can use ActivityLifecycleCallbacks to achieve this. There's an example with some logs below.
public class MyApplication extends Application {
private static final String TAG = MyApplication.class.getSimpleName();
private int mVisibleCount;
private boolean mInBackground;
#Override public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
#Override public void onActivityStarted(Activity activity) {
mVisibleCount++;
if (mInBackground && mVisibleCount > 0) {
mInBackground = false;
Log.i(TAG, "App in foreground");
}
}
#Override public void onActivityResumed(Activity activity) {
}
#Override public void onActivityPaused(Activity activity) {
}
#Override public void onActivityStopped(Activity activity) {
mVisibleCount--;
if (mVisibleCount == 0) {
if (activity.isFinishing()) {
Log.i(TAG, "App is finishing");
} else {
mInBackground = true;
Log.i(TAG, "App in background");
}
}
}
#Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
#Override public void onActivityDestroyed(Activity activity) {
}
});
}
public boolean isAppInBackground() {
return mInBackground;
}
public boolean isAppVisible() {
return mVisibleCount > 0;
}
public int getVisibleCount() {
return mVisibleCount;
}
}
Method 2: There's another method using Service to detect if application is terminated. See link
Following method call on diff action
Minimize the Application using home button --> this calls
onPause()
onStop()
Remove the app from Task Manager ->
then onDestroy() is called for that MainActivity (launcher).
So Make an parent Activity suppose named BaseActivity and override its
onPause(), onDestroy() and onStop() method and call your
implementation from here and then extend this Activity from your
another Activities
You should not rely on the onDestroy method. As per the official android documentation it is not called always.
Try to put the same code in the onStop() method of your activity.
Additionally you can have a parent activity which is extended by all other activities.
You can then override the onPause() and onStop() method in your particular child activities and handle the specific scenarios.

Chromecast: how to select a CastDevice without a CastButton

I'm working on an App where i want to stream a specific video, which is located on my mobile device. I want to stream this video with the help of ChromeCast to a projector. I've done the Google Chromecast Tutorials (https://codelabs.developers.google.com/codelabs/cast-videos-android/#0 for example) and already looked for an answer to my Question but there wasn't the perfect answer to solve my problem in my opinion.
There are a few solutions in other coding languages but not especially for java.
My problem with the Google Cast API is: I want to select my ChromeCast without pressing the Cast Button in the OptionMenu, which appears to select one of the ChromeCasts in the Network.
I just found out, that the setRouteSelector-Methode from the class CastButtonFactory is a part to select the Device. But i can't find out, how to select the device automatically.
Here is my Code where the CastButton appears (created in the OnCreateOptionsMenu-Method)(It's just the Code to Connect to the ChromeCast. I didn't implement the Code for selecting and streaming the video yet):
Can anyone help me?
Code:
public class MainActivity extends AppCompatActivity {
private CastSession mCastSession;
private CastContext mCastContext;
private SessionManager mSessionManager;
private final SessionManagerListener mSessionManagerListener = new SessionManagerListenerImpl();
private class SessionManagerListenerImpl implements SessionManagerListener{
#Override
public void onSessionStarting(Session session) {}
#Override
public void onSessionStarted(Session session, String sessionId) {
invalidateOptionsMenu();
}
#Override
public void onSessionStartFailed(Session session, int i) {}
#Override
public void onSessionEnding(Session session) {}
#Override
public void onSessionEnded(Session session, int error) {
finish();
}
#Override
public void onSessionResuming(Session session, String s) {}
#Override
public void onSessionResumed(Session session, boolean wasSuspended) {
invalidateOptionsMenu();
}
#Override
public void onSessionResumeFailed(Session session, int i) {}
#Override
public void onSessionSuspended(Session session, int i) {}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCastContext = CastContext.getSharedInstance(this);
mCastContext.registerLifecycleCallbacksBeforeIceCreamSandwich(this, savedInstanceState);
mSessionManager = CastContext.getSharedInstance(this).getSessionManager();
}
#Override
public void onResume(){
mCastSession = mCastContext.getSessionManager().getCurrentCastSession();
mSessionManager.addSessionManagerListener(mSessionManagerListener);
super.onResume();
}
#Override
public void onPause(){
super.onPause();
mSessionManager.removeSessionManagerListener(mSessionManagerListener);
mCastSession = null;
}
#Override
public void onStart(){
super.onStart();
}
#Override public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(),
menu,
R.id.media_route_menu_item);
return true;
}}
This is possible with SDK v2. Check out the source code for MediaRouteChooserDialogFragment, the UI element that lets users select a Cast device. You'll want to create a MediaRouter.Callback, and override onRouteAdded(MediaRouter router, MediaRouter.RouteInfo newRoute). If the new route is not the default route, select it with mMediaRouter.selectRoute(newRoute). This will connect you to the receiver device without any user interaction.
This should work for testing, but please don't do this in a production app. #AliNaddaf correctly notes this is against the UX guidelines and Google Cast terms of service (you can imagine the potential for abuse if apps are permitted to start casting without the user's consent).

Opens hundreds of tabs on single ad-click

listener = new RevMobAdsListener() {
#Override
public void onRevMobAdClicked() {
Log.i("[RevMob]", "Advertisement Clicked!");
revmob.openAdLink(application, APPLICATION_ID, this);
return;
}
#Override
public void onRevMobAdDismiss() {
Log.i("[RevMob]", "Advertisement Closed!");
fullscreenAd.hide();
}
#Override
public void onRevMobAdDisplayed() {
Log.i("[RevMob]", "Advertisement Displayed!");
}
#Override
public void onRevMobAdNotReceived(String message) {
Log.i("[RevMob]", "No Advertisement Available!");
}
#Override
public void onRevMobAdReceived() {
Log.i("[RevMob]", "Advertisement Pulled from network!");
}
#Override
public void onRevMobSessionIsStarted() {}
#Override
public void onRevMobSessionNotStarted(String arg0) {}
};
So what the problem is, is that once I click on the advertisement it continuously opens tabs in my browser.
LogCat spams the following debug messages. (In order)
Advertisment Pulled from network!
Advertisement Displayed!
Reporting impression using testing mode: with_ads
Advertisement Clicked!
^The above is apmmed on repeat, everytime it opens a new tab
Look at your first listener (onRevMobAdClicked): when you click the first ad, this listener is called, and it calls an adlink.
By default (from RevMob SDK), when called (openAdLink), the link calls automatically onRevMobAdReceived, onRevMobAdDisplayed and onRevMobAdClicked listeners.
Then, this adlink is opened and it fires this same listener (onRevMobAdClicked), calling again the adlink, which will fire the listener, which will call again the adlink, ..........
You have to change the way you call the link.
Also, you don't need to call fullscreen.hide() in onRevMobAdDismiss. When the fullscreen is dismissed by the user, this event is fired.
Hope that helps!

Admob Interstitial Ads showing in the middle of the game

I just made game like ironpants, I'm trying to put interstitial ads when the game over.
but the interstitial ads always show in the middle of the game. I think it'll be annoying for users. and I'm afraid if Google will banned my admob account.
The interstitial ads only show after 5x game over.
I only have onescreen MainGameView.java
here's the code to show admob interstitial ads when the game was over
the methods to initialize the interstitial ads
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initialiseAccelerometer();
// Create the interstitial
interstitial = new InterstitialAd(this, getResources().getString(R.string.InterstitialAd_unit_id));
}
public void openAd() {
runOnUiThread(new Runnable() {
public void run() {
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(new AdListener() {
#Override
public void onReceiveAd(Ad arg0) {
if (interstitial.isReady()) {
interstitial.show();
}
}
#Override
public void onPresentScreen(Ad arg0) {
}
#Override
public void onLeaveApplication(Ad arg0) {
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
}
#Override
public void onDismissScreen(Ad arg0) {
}
});
}
});
}
and I call openAd() method when the game was over
public synchronized void GameOver() {
if (adounter >= this.getResources().getInteger(R.integer.add_shows_every_X_gameovers)) {
openAd();
adounter = 0;
}
adounter++;
}
thank you very much and sorry for my bad English.
Your problem is that you are showing the interstitial d as soon as it is received (ie in AdListener#onReceiveAd). Don't do this. It provides a poor user experience and will ensure you get low ratings.
Instead show your add at a natural break in your app.
Instead show your add at a natural break in your app.
Exactly, to be more precise it would be good user experience if you do the following:
- request for the ad in onCreate
- in GameOver call:
if (interstitial.isReady()) {
interstitial.show();
}

Categories

Resources