Airpush SmartWall "clicking" itself whenever loading is completed, using LibGDX - java

I have been struggling with this problem for a while now and would be very grateful if anyone could help me out.
The problem is that when I´ve successfully loaded an airpush "smartwall" it "clicks" itself and I immediately go to wherever the ad takes you. Before the ad has completed loading I can see the graphical representation of the ad loading.
I´ve contacted AP-support several times but I´m still having the same problem no matter what I do.
The problem seems to be that the smartwall is running on the wrong thread, I am using a UIThread handler for other stuff and it works fine (leadbolt interstitials, alertdialogs and other native android stuff).
Things I´ve tried:
AsyncTask
#Override
public void showAP() {
uiThread.post(new Runnable(){
public void run(){
new Async().execute();
}
});
}
private class Async extends AsyncTask<String, Integer, String[]>{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String[] doInBackground(String... params) {
if(apSmartWall == null)
apSmartWall = new ApSmartWall(MainActivity.this, adCallbackListener);
apSmartWall.startSmartWallAd();
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String[] result) {
super.onPostExecute(result);
}
}
Handler
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
apSmartWall = new ApSmartWall(MainActivity.this, adCallbackListener);
// Create the layout
layout = new RelativeLayout(this);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libgdx View
gameView = initializeForView(new Game(this), true);
// Add the libgdx view
layout.addView(gameView);
context = this;
uiThread = new Handler();
... (adding another view for mopub here, but removing this doesn´t change anything)
// Hook it all up
setContentView(layout);
}
#Override
public void showAP() {
uiThread.post(new Runnable(){
public void run(){
apSmartWall.startSmartWallAd();
}
});
}
libGDX Thread stuff: https://code.google.com/p/libgdx/wiki/TheArchitecture#The_Graphics_Module
I also tried using libGDX built in handler method, AndroidApplication.runOnUiThread() but that didn´t work either. I am by no means experienced when it comes to native Android so there might be something I´ve done wrong. But like I said it´s working fine for other stuff that runs on the UI Thread, for example this works fine:
#Override
public void resumeAds() {
uiThread.post(new Runnable(){
public void run() {
moPubView = new MoPubView(context);
// non-tablet
if(width >= 728)
moPubView.setAdUnitId(TABLET_SIZE);
else
moPubView.setAdUnitId(PHONE_SIZE);
moPubView.loadAd();
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(moPubView, adParams);
}
});
}
AndroidManifest.xml airpush things
<meta-data android:name="com.package.APPID" android:value="appid" />
<meta-data android:name="com.package.APIKEY" android:value="android*key"/>
<activity android:exported="false" android:name="com.package.SmartWallActivity"
android:configChanges="orientation|screenSize"
android:theme="#android:style/Theme.Translucent" />
<activity android:name="com.package.BrowserActivity"
android:configChanges="orientation|screenSize" />

Related

Can the text of an Android TextView be set outside of the onCreate() method?

I'm trying to set the text of a TextView in my Android app using the following function:
#Override
public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
if (!stateChanges.getFrom().getSubscribed() && stateChanges.getTo().getSubscribed()) {
new AlertDialog.Builder(this)
.setMessage("You have successfully subscribed to push notifications!")
.show();
// Get player ID and output to Main Activity
TextView playerIdView = findViewById(R.id.playerIdView);
playerIdView.setText(stateChanges.getTo().getUserId());
}
Log.i("Debug", "onOSPermissionsChanged: " + stateChanges);
}
This uses the OneSignal API to get the user's unique ID, which is returned as a string. After some debugging I realised the contents of a TextView can't be changed outside of the onCreate() method. However, the stateChanges parameter is required, which only exists within onOSSubscriptionChanged. Is there any way of getting around this?
EDIT: the error was elsewhere. stateChanges.getTo().getUserId() was returning null.
You need to set it on UI thread
playerIdView.post(new Runnable() {
public void run() {
playerIdView.setText(stateChanges.getTo().getUserId());
}
});
or
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = new Runnable() {
#Override
public void run() {
playerIdView.setText(stateChanges.getTo().getUserId());}
};
mainHandler.post(myRunnable);
you need to initialize you textview in your onCreateView() method and after that you can use that textView pretty much anywhere as long as you are in UI thread. So change your code to below:
Declare your textview globally so that you can use it anywhere in your activity instance.
TextView playerIdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
playerIdView = findViewById(R.id.playerIdView);
}
and then in your onSSubscription method just do the following:
#Override
public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
if (!stateChanges.getFrom().getSubscribed() && stateChanges.getTo().getSubscribed()) {
new AlertDialog.Builder(this)
.setMessage("You have successfully subscribed to push notifications!")
.show();
// Get player ID and output to Main Activity
playerIdView.setText(stateChanges.getTo().getUserId());
}
Log.i("Debug", "onOSPermissionsChanged: " + stateChanges);
}
Try This
runOnUiThread(new Runnable(){
#override
public void run() {
playerIdView.setText(stateChanges.getTo().getUserId());
}
})

Frosty glass background effect in android activity

I am trying to apply a frosty glass effect to the background image of an activity, but I don't know how to do this can anyone know or do this kind of application if yes then help me please.
Expected output:-
This is my code where I am using Blurry library but when the application run it does work as expected:-
public class SplashActivity extends AppCompatActivity {
long delay = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
Blurry.with(getApplicationContext())
.radius(25)
.sampling(2)
.async()
.animate(500)
.capture(findViewById(R.id.content))
.into((ImageView) findViewById(R.id.content));
Timer runSplash = new Timer();
TimerTask showSplash = new TimerTask() {
#Override
public void run() {
finish();
startActivity(new Intent(SplashActivity.this,MainActivity.class));
}
};
runSplash.schedule(showSplash,delay);
}
}
May be this library is helpful to you.
https://android-arsenal.com/details/1/2192

Android Java: Update seekbar to show progress as audio file plays

This is my first Android/Java app. I am using the first answer here to try to initiate a repeating task, updating a seekbar ("timeSlider") to show progress as an audio file plays. Here is my code (eliminating a few irrelevant lines):
private int timeSliderInterval = 1000; // 1 second
private Handler timeSliderHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
Intent intent = getIntent();
Runnable doUpdateTimeSlider = new Runnable() {
#Override
public void run() {
timeSliderHandler.postDelayed(doUpdateTimeSlider, timeSliderInterval);
updateTimeSlider();
}
};
void startUpdateTimeSlider() {
doUpdateTimeSlider.run();
}
void stopUpdateTimeSlider() {
timeSliderHandler.removeCallbacks(doUpdateTimeSlider);
}
final SeekBar timeSlider = (SeekBar) findViewById(R.id.timeSlider);
if (timeSlider != null) {
timeSliderHandler = new Handler();
startUpdateTimeSlider();
}
#Override
public void onDestroy() {
super.onDestroy();
stopUpdateTimeSlider();
}
The project does not display in the emulator. Tooltips show these errors:
In addition, the startUpdateTimeSlider and stopUpdateTimeSlider functions are showing this error in tooltips:
Also, in the Run window, I'm getting:
emulator: emulator window was out of view and was recentered
emulator: ERROR: _factory_client_recv: Unknown camera factory query
name in ' '
Any help will be greatly appreciated!
First issue is self explained, you need to add final modifier.
final Runnable doUpdateTimeSlider = ...
Second one - move startUpdateTimerSlider() method, now its inside onCreate() method
Looks like you missed }:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
Intent intent = getIntent();
Runnable doUpdateTimeSlider = new Runnable() {
#Override
public void run() {
timeSliderHandler.postDelayed(doUpdateTimeSlider, timeSliderInterval);
updateTimeSlider();
}
};
}//<--------HERE
void startUpdateTimeSlider() {
doUpdateTimeSlider.run();
}

Android: Media Player not pausing in second activity using ImageView

I have spent almost the day trying to figure it out and yet could not manage to achieve it. I basically have two activities, MainActivity.java and GameActivity.java. The music stops but after going back to MainActivity.java and coming back to GameActivity.java using ImageView several times.
MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageView iv;
public static final String BG_SOUND_CHECK = "background playing!";
// public static MediaPlayer backgroundSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
final MediaPlayer backgroundSound = MediaPlayer.create(this, R.raw.background_music);
handler.postDelayed(new Runnable() {
#Override
public void run() {
backgroundSound.setLooping(true);
backgroundSound.start();
Log.v(BG_SOUND_CHECK, "After loop Started!");
}
}, 3000);
getSupportActionBar().hide();
iv = (ImageView)findViewById(R.id.playImage);
iv.setOnClickListener(new View.OnClickListener(){
public void onClick(View view1){
// backgroundSound.setLooping(false);
backgroundSound.pause();
// Log.v(BG_SOUND_CHECK, "Playing After Play Button Click");
Intent myIntent = new Intent(MainActivity.this, GameActivity.class);
// myIntent.putExtra("backgroundSoundObj",backgroundSound);
startActivity(myIntent);
// Log.v(BG_SOUND_CHECK, "After Game Scene");
}
});
}
}
GameActivity.java
public class GameActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
// MainActivity.backgroundSound.pause();
// Rest of the code
}
}
I tried putting the music onPause(); in
1) ImageView onClick();
2) As soon as the GameActivity.java is loaded
but both ways end up pausing it only after I try coming back to MainActivity.java and going to GameActivity.java several times.
Do you see how you put your backgroundSound.start() inside the postDelayed-3000 ?
backgroundSound.pause() anytime before that 3000ms is not going to do anything.
It has nothing to do with going back and forth between your two activities, it just so happens that doing that takes up more than 3000ms, so when you do click for pause(), 3000ms has passed.
What you can do, to fit your current code, is to separate that into a Runnable.
Runnable bgSound = new Runnable() {
#Override
public void run() {
backgroundSound.setLooping(true);
backgroundSound.start();
Log.v(BG_SOUND_CHECK, "After loop Started!");
}
}
Use it with handler.postDelayed(bgSound, 3000)
In your onClick, if you want to stop it from playing before it starts, can use handler.removeCallbacks(bgSound), to stop it after it plays, your pause(). If you don't care to know if it has or hasn't started, you can do both alongside each other, should work fine too.
(if you don't want the music to play after you've left your main activity, you should also do the same actions in your onDestroy and/or onPause)

Why doesnt ImageView work for me? Android

My code is the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_game);
//initialize arks
new Timer().schedule(new TimerTask() {
#Override
public void run() {
initializeArks();
}
}, 2000);
}
Nothing special there, here comes the initializeArks():
public void initializeArks(){
iv= (ImageView) findViewById(R.id.imageView);
tw= (TypeWriter) findViewById(R.id.textView);
CountDownTimer timer = new CountDownTimer(10, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
iv.setImageResource(R.drawable.loading);
}
}.start();
}
Keep in mind that this code is only for debugging, it doesnt do anything specific, but as I found out the problem is with the ImageView
Also i have my variables declared STATIC for the purpose of using them in my other classes:
public static ImageView iv;
public static TypeWriter tw;
My XML is a basic one:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
I would be grateful if somebody explained me why this doesnt work or would send me a working code snippet for me to use for only one purpose: displaying images.
Once i install the app on my phone and open it I wait exactly 4 seconds (thats how much im delaying it by yhe code above) and it simply crashes
I think that you should be execute the line
iv.setImageResource(R.drawable.loading);
inside UI thead. Look at this answer
Initialise your imageview in onCreate() method not in initializeArks() method and dont declare them as static like as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_game);
iv= (ImageView) findViewById(R.id.imageView); //<==== initialise in onCreate
tw= (TypeWriter) findViewById(R.id.textView);
//initialize arks
new Timer().schedule(new TimerTask() {
#Override
public void run() {
initializeArks();
}
}, 2000);
}
And declared variable as
public ImageView iv;
public TypeWriter tw;
I couldnt find a way to achive what I wanted so I looked in the big picture, and my scenario allowed me to not have an ImageView. Fortunately changing the layout background does the trick for me and it doesnt have any negative effects for my project. Thank you for your wasted time here
It seems like you do not run on the UI Thread.
Try to run inside your thread like this:
runOnUiThread(new Runnable() {
#Override
public void run() {
initializeArks();
}
});

Categories

Resources