PostDialogListener() android Facebook api issue - java

I am trying to create a small app that posts the entries made by an user in my app to facebook when they press the back button to quit. I already have SSO and everything else set up, the problem is it does not recognize PostDialogListener as a valid listener. Says does not exist, the code where I am trying to use it
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
fb.dialog(getApplicationContext(), "stream.publish", new PostDialogListener());
Toast t=Toast.makeText(getApplicationContext(), " Back pressed ", Toast.LENGTH_LONG);
t.show();
}
return super.onKeyDown(keyCode, event);
}
I tried putting the dialog code in other places too, but same result, PostDialogListener is not being recognized.

You probably don't import it from example. Add these classes to your project:
public abstract class BaseDialogListener implements DialogListener {
#Override
public void onFacebookError(FacebookError e) {
e.printStackTrace();
}
#Override
public void onError(DialogError e) {
e.printStackTrace();
}
#Override
public void onCancel() {
}
}
public class PostDialogListener extends BaseDialogListener {
#Override
public void onComplete(Bundle values) {
final String postId = values.getString("post_id");
if (postId != null) {
showToast("Message posted on the wall.");
} else {
showToast("No message posted on the wall.");
}
}
}

Related

How can I refresh the listview after I delete my draft when it automatically re-presses?

How can i refresh listview after I delete my draft when it automatically back pressed?
public void deleteDraft() {
((LoaderActivity) getActivity()).getLoader().show();
ApiHelper.get().itrDeleteDraft(new ItrDeleteDraftApi.Request(userID, projectID, itrRaisedID)).enqueue(new ApiRequest.Callback<ItrDeleteDraftApi.Response>() {
#Override
public void onSuccess(ItrDeleteDraftApi.Response response) {
((LoaderActivity) requireActivity()).getLoader().hide();
requireActivity().onBackPressed();
}
#Override
public void onFailure(String e) {
((LoaderActivity) getActivity()).getLoader().hide();
ErrorManager.promptError(getActivity(), e);
}
});
}
#OnClick(R.id.buttons_itr_delete) public void onDeleteButtonClick(){
new AlertDialog.Builder(getContext()).setCancelable(false).setTitle("").setMessage(R.string.confirm_delete_itr).setNegativeButton(R.string.cancel, (d,e)->d.dismiss()).setPositiveButton(R.string.ok, (d,e)-> {
itrDetailFragment.deleteDraft();
}).create().show();
}
It will refresh or else it will disappear in the list

Show a progress Bar when Rewarded video Ads is loading

I want to use Rewarded video ads (Admob) but I want to show a progress bar while the video ads is loading
I already try to did it with async task just to see if the video will load but it didn't work
#SuppressLint("StaticFieldLeak")
public class videoAd extends AsyncTask<Void, Void, Void> {
#Override
protected void doInBackground(Void... voids) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
}
});
}
#Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if (mRewardedVideoAd.isLoaded()){
Toast.makeText(SetFullWallpaper.this, "Video loaded", Toast.LENGTH_SHORT).show();
mRewardedVideoAd.show();
}
}
}
Now I want to load a progress bar if the video is not loaded yet
Thank you
This is how I did it:
I had a button, which when clicked showed the ad, so I had a boolean variable which tracked whether the button has been clicked:
boolean buttonClicked = false
These lines were in my onCreate function:
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(getContext());
rewardedVideoAdListener = new RewardedVideoAdListener() {
#Override
public void onRewardedVideoAdLoaded() {
if(buttonClicked) {
showAd();
}
}
#Override
public void onRewardedVideoAdOpened() {
}
#Override
public void onRewardedVideoStarted() {
}
#Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
if(buttonClicked) {
progressBar.setVisibility(View.INVISIBLE);
Toast toast = Toast.makeText(getContext(), "Please try again later", Toast.LENGTH_SHORT);
toast.show();
}
}
#Override
public void onRewardedVideoCompleted() {
}
};
mRewardedVideoAd.setRewardedVideoAdListener(rewardedVideoAdListener);
loadRewardedVideoAd();
pointsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAd();
}
});
This was my showAd function:
public void showAd(){
if (mRewardedVideoAd.isLoaded()) {
progressBar.setVisibility(View.GONE);
mRewardedVideoAd.show();
buttonClicked = false;
} else {
loadRewardedVideoAd();
progressBar.setVisibility(View.VISIBLE);
buttonClicked = true;
}
}
How this works is, the app tries to load the ad in the background by calling the loadRewaredVideoAd() function when the activity/fragment is created. Then when the user clicks the button,showAd() function is called and one of two things happen:
1) If the ad was successfully loaded, it shows the ad.
2) If not, it calls loadRewardedVideoAd() again and shows a progressbar this time. It also sets buttonClicked to true. Then if the ad loads, the onRewardedVideoAdLoaded() function is called which calls showAd() again and this time the 1st option happens.
If the ad didn't load this time as well, then onRewardedVideoAdFailedToLoad(int i)is called and it shows a toast saying the user to try again later.
add OnProgressUpdate() in your class that extends AsyncTask and add progress dialog in this.
I am placing an admob video, which is executed when you enter the activity. In my case, I put an executable loop to start the video when it is already loaded.
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}else{
loadRewardedVideoAd();
}
This is part of all my code
private boolean started = false;
private Handler handler = new Handler();
private void initializeAdMob() {
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
}
private void loadRewardedVideoAd() {
if (!mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.loadAd(getString(R.string.id_block_activity), new AdRequest.Builder().build());
}
}
private void showRewardedVideoAd() {
if (BaseInteractor.isNetworkAvailable(this)) {
showProgressBar(true);
start();
}
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}else{
loadRewardedVideoAd();
}
if (started) {
start();
}
}
};
public void stop() {
started = false;
handler.removeCallbacks(runnable);
}
public void start() {
started = true;
handler.postDelayed(runnable, 2000);
}
finally I stop the runnable in
#Override
public void onRewardedVideoAdOpened() {
showProgressBar(false);
stop();
}
I hope this helps you.

How to get play and stop event callback from chromecast android

I want to get callback event and perform some function when chromecast audio change there playback mode(play/stop)
How can I get the play/stop event to the application so I will do my work on that event.
Please check below logic I have implemented.
private void setupCastListener() {
mSessionManagerListener = new SessionManagerListener<CastSession>() {
#Override
public void onSessionEnded(CastSession session, int error) {
onApplicationDisconnected();
}
#Override
public void onSessionResumed(CastSession session, boolean wasSuspended) {
onApplicationConnected(session);
}
#Override
public void onSessionResumeFailed(CastSession session, int error) {
onApplicationDisconnected();
}
#Override
public void onSessionStarted(CastSession session, String sessionId) {
onApplicationConnected(session);
}
#Override
public void onSessionStartFailed(CastSession session, int error) {
onApplicationDisconnected();
}
#Override
public void onSessionStarting(CastSession session) {
}
#Override
public void onSessionEnding(CastSession session) {
}
#Override
public void onSessionResuming(CastSession session, String sessionId) {
if(mCastSession!=null && isChromeCastConnected){
try {
if (session.isMute()) {
mStopPlayButton.setImageResource(R.drawable.ic_play);
isChromeCastPlay = false;
//mCastSession.setMute(!mCastSession.isMute());
} else {
mStopPlayButton.setImageResource(R.drawable.ic_stop);
isChromeCastPlay = true;
//mCastSession.setMute(!mCastSession.isMute());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onSessionSuspended(CastSession session, int reason) {
}
};
}
Please let me know. thanks
You need to read this documentation, and focus on RemoteMediaClient and the Listener interface there. The callback onStatusUpdated() will be called when there is a change in the playback status. Tutorials available on the first link above is very informative, so make sure you read about things there.
Finally I found the remote media play and pause mode call back by below MediaControlIntent.
Remote Playback Routes
mMediaRouter = MediaRouter.getInstance(this);
mSelector = new MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build();

Back button press twice before quitting app

How can I configure the back button to be pressed twice before the app exits? I want to trigger
#Override
public void onBackPressed() {
//custom actions
//display toast "press again to quit app"
super.onBackPressed();
}
Try this:
private boolean doubleBackToExitPressedOnce = false;
#Override
protected void onResume() {
super.onResume();
// .... other stuff in my onResume ....
this.doubleBackToExitPressedOnce = false;
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press twice to exit", Toast.LENGTH_SHORT).show();
}
This snippet handle also the reset state when the activityis resumed
I see this question is a bit old but I though this might help some people looking for an alternative to the answers already given.
This is how I handle backing out of my applications. If someone has a better -- or a Google suggested -- method of accomplishing this I'd like to know.
Edit -- Forgot to mention this is for Android 2.0 and up. For previous versions override onKeyDown(int keyCode, KeyEvent event) and check for keyCode == KeyEvent.KEYCODE_BACK. Here is a good link to check out.
private boolean mIsBackEligible = false;
#Override
public void onBackPressed() {
if (mIsBackEligible) {
super.onBackPressed();
} else {
mIsBackEligible = true;
new Runnable() {
// Spin up new runnable to reset the mIsBackEnabled var after 3 seconds
#Override
public void run() {
CountDownTimer cdt = new CountDownTimer(3000, 3000) {
#Override
public void onTick(long millisUntilFinished) {
// I don't want to do anything onTick
}
#Override
public void onFinish() {
mIsBackEligible = false;
}
}.start();
}
}.run(); // End Runnable()
Toast.makeText(this.getApplicationContext(),
"Press back once more to exit", Toast.LENGTH_SHORT).show();
}
}
You could do what you're asking with a global integer and just count it, if > 2, quit.
But you could take a better (IMO) approach, where you question the user if they would like to quit or not:
private void questionQuit(){
final CharSequence[] items = {"Yes, quit now", "No, cancel and go back"};
builder = new AlertDialog.Builder(mContext);
builder.setCancelable(false);
builder.setTitle("Are you sure you want to quit?");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0:
quit();
break;
case 1:
default:
break;
}
}
}).show();
AlertDialog alert = builder.create();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK :
int i = 0 ;
if(i == 1 )
{
finish();
}
i++;
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Scope Variable issue for onKeyDown event

I am having trouble using the var "str" in an if statement. I know that it is a scope problem but I am not sure how to fix it. I have failed several times.
My goal is to use the value of "str" in an if statement to show an alert or not.
Restriction is I have to assign the value of "str" this way only.
public class MyJavaScriptInterface
{
public String showHTML(String html)
{
String str = html;
return str;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK && str =="0")
{
//Ask the user if they want to quit
Update:
OK I was try not to bother you with reading the whole code but it's short and it might help.
In a webview I show the html page and I also read a hidden variable which will decide if an alert box should show or not. I am just trying to get that value to the if statement in the keydown push. Here is the whole code.
package com.ishop.pizzaoven;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class buttonOne extends Activity
{
WebView wb = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.buttons);
wb = new WebView(this);
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
wb.getSettings().setJavaScriptEnabled(true);
wb.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
/* WebViewClient must be set BEFORE calling loadUrl! */
wb.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
wb.loadUrl("javascript:window.HTMLOUT.showHTML(document.getElementById('sendtextcoupon').value);");
}
});
wb.loadUrl("http://ishopstark.com/mobileapp.php?category=1");
setContentView(wb);
}
private class HelloWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
final Context myApp = this;
public class MyJavaScriptInterface
{
public void showHTML(String html)
{
String str = html;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK)
{
//Ask the user if they want to quit
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Coupon")
.setMessage("Do you want a coupon texted to you?")
.setPositiveButton("YES!", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//Stop the activity
Toast.makeText(buttonOne.this, "Great! You'll get one in just a couple of minutes.", Toast.LENGTH_LONG).show();
finish();
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//Stop the activity
finish();
}
})
.show();
return true;
}
else
{
return super.onKeyDown(keyCode, event);
}
}
}// End of main class
Update 2:
ok so i changed my code a bit. But now I get alert errors any ideas? "The method setIcon(int) is undefined for the type buttonOne.MyJavaScriptInterface" I get this error on .setIcon(android.R.drawable.ic_dialog_alert) and return super.onKeyDown(keyCode, event);
public class MyJavaScriptInterface
{
public String showHTML(String html)
{
String str = html;
return str;
}
public boolean onKeyDown(int keyCode, KeyEvent event, String str)
{
//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK && str == "0")
{
//Ask the user if they want to quit
AlertDialog.Builder alertbox = new AlertDialog.Builder(buttonOne.this);
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Coupon")
.setMessage("Do you want a coupon texted to you?")
.setPositiveButton("YES!", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//Stop the activity
Toast.makeText(buttonOne.this, "Great! You'll get one in just a couple of minutes.", Toast.LENGTH_LONG).show();
finish();
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//Stop the activity
finish();
}
})
.show();
return true;
}
else
{
return super.onKeyDown(keyCode, event);
}
}
}
Not sure exactly what your aim is, but can you not make the string a member variable?
public class MyJavaScriptInterface
{
private String str;
public String showHTML(String html)
{
str = html;
return str;
}
}
It would be helpful to know which class the listener method is in; you have it outside the MyJavaScriptInterface class, but don't have any context around it. So when onKeyDown happens, do you have the html in order to run the showHTML(html) method? Has it already been run? Do you have an instance of MyJavaScriptInterface to run it on?
If you have an extra } in there and it is indeed a part of the same class, then I would agree with #Steve's answer.
If it's outside the class, then there are many solutions depending upon your actual situation. The simplest way would be to make str an instance variable as #Steve said, and then add an accessor method:
public String getStr(){
return str;
}
and then have your listener call that accessor instead of attempting to use str directly:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// Get an instance of MyJavaScriptInterface;
MyJavaScriptInterface mjsi = new MyJavaScriptInterface();
//Handle the back button
if( keyCode == KeyEvent.KEYCODE_BACK && mjsi.getStr().equals("0") )
{
//Ask the user if they want to quit

Categories