When I pressed back button on the mobile phone after that I switch off the wifi or mobile data network.
Progress bar spinning does not get stop and back button does not work.
protected void onRefreshingStateChanged(boolean refreshing) {
super.onRefreshingStateChanged(refreshing);
if (!refreshing){
if (mProgressDialog != null && isBackPressed){
mProgressDialog.hide();
mProgressDialog = null;
this.setResult(RESULT_OK, getIntent());
finish();
}
}else{
if (mProgressDialog == null && isBackPressed){
mProgressDialog = ProgressDialog.show(this, "Fetching Data",
"Please wait while we are updating records ...", true);
}
}
}
try to set cancelable property of progressbar.
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setCancelable(true);
Related
I have made an android app using Android Studio and I have implemented Google AdMob into it.
However, when it comes to showing ads, whenever I try to show it from a function, it does not show.
Here is the function:
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.jodastudios.universalskyremote", Context.MODE_PRIVATE);
int clicks = sharedPreferences.getInt("clicks", 0);
sharedPreferences.edit().putInt("clicks", 0);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}
Whenever I show the ads on the OnCreate method, they show.
Here is the code:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.jodastudios.universalskyremote", Context.MODE_PRIVATE);
int clicks = sharedPreferences.getInt("clicks", 0);
sharedPreferences.edit().putInt("clicks", 0);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}
},9000);
I don't know why the ads are not showing from a function: does anybody know how to fix this?
Second code is delayed for 9 sec and that's enough time for the ad to load.
Function cannot display ad that has not been loaded!
You have to set AdListener and show add from onAdLoaded method or create callback to show from activity...
...or call your function from onAdLoaded(){
InterstitialAd interstitialAd = new InterstitialAd(context);
interstitialAd.setAdUnitId(adIdent);
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
interstitialAd.show();
//TODO or create callback to show from activity
if (adCallback != null) {
adCallback.onAdLoaded();
}
}
.....................
.....................
}
and
interstitialAd.loadAd(new AdRequest.Builder().build());
So I try to make a app that will test if a phone can call phone numbers. And the main point is that when i push a call button and when the connection begins app ends a call and if there is no connection app shows an error. I managed to do a part where when button is clicked app calls a phone number but I can't come up with solution for ending a cal when there is connection (ringing). Is there any way to make this possible?
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String phone = "8686868686";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.fromParts("tel", phone, null));
startActivity(intent);
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
/*Context mContext = getApplicationContext();
TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
if (tm != null) {
boolean success = tm.endCall();
// success == true if call was terminated.
}*/
}
});
I want to "click" the "Exit Navigation" button that appear in the Google Navigation notification from my android application code.
Google Navigation notification:
I know that there is a way to do it because I can do it using a Tasker task, but I don't know how to do it using code from my application.
I have a NotificationListenerService and I can retrieve the text of the notification when onNotificationPosted is received with the following code:
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Notification notification = sbn.getNotification();
if (!pack.equals("com.google.android.apps.maps")) return;
RemoteViews views = notification.bigContentView;
if (views == null) views = notification.contentView;
if (views == null) return null;
int viewid=0;
List<String> text = new ArrayList<String>();
try
{
Field fieldActions = views.getClass().getDeclaredField("mActions");
fieldActions.setAccessible(true);
#SuppressWarnings("unchecked")
ArrayList<Parcelable> actions = (ArrayList<Parcelable>) fieldActions.get(views);
for (Parcelable p : actions)
{
Parcel parcel = Parcel.obtain();
p.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
int tag = parcel.readInt();
if (tag != 2) continue;
viewid=parcel.readInt();
String methodName = parcel.readString();
if (methodName == null) continue;
else{
if (methodName.equals("setText"))
{
parcel.readInt();
String t = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel).toString().trim();
text.add(t);
Log.w(TAG,"viewID: "+viewid+"setText: "+t);
}
}
parcel.recycle();
}
} catch (Exception e)
{
Log.e(TAG, e.toString());
}
return text;
}
But I didn't find a way to retrieve the actions ("buttons") of the notification.
Thanks!
It's quite simple. Just get the Action Buttons from that notification like this.
Notification notification = sbn.getNotification();
Notification.Action[] actions = notification.actions;
And because this notification has only one action button, you simply do:
actions[0].actionIntent.send();
AFTER you checked that's a Google Maps notification. You did this here:
if (!pack.equals("com.google.android.apps.maps")) return;
So complete code should look like this:
Notification notification = sbn.getNotification();
Notification.Action[] actions = notification.actions;
if (!pack.equals("com.google.android.apps.maps")) return;
actions[0].actionIntent.send();
Should work. Hope I can help you :)
I'm a new android developer, suffering from a problem recently.
The background is that i need to show an AlertDialog when a back-end asyncTask finished. However the activity may be GC'd after a long time asyncTask, so that the context tobe input param of AlertDialog is null.Is there any workaround to solve this problem.
I use this function to show dialog:
public static Dialog showDialog(
Context ctx, int themeId, String title, String message,
int okStrId, android.content.DialogInterface.OnClickListener okListener,
int cancelStrId, android.content.DialogInterface.OnClickListener cancelListener) {
if (ctx != null) {
AlertDialog.Builder builder;
if (themeId > 0)
builder = new AlertDialog.Builder(new ContextThemeWrapper(ctx, themeId));
else
builder = new AlertDialog.Builder(ctx);
if (title != null)
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(ctx.getString((okStrId < 0) ? R.string.ok : okStrId),
(okListener != null) ? okListener : sDefaultDialogListener);
if (cancelListener != null)
builder.setNegativeButton(ctx.getString((cancelStrId < 0) ? R.string.cancel : cancelStrId), cancelListener);
else {
builder.setCancelable(false);
}
AlertDialog ad = builder.create();
ad.show();
return ad;
}else {
Context context = SuccessFactorsApp.getAppContext();
DialogActivity.launchActivity(ctx,themeId,title,message,okStrId,okListener,cancelStrId,cancelListener);
return new AlertDialog.Builder(context).create();
}
}
I tried to use an Activity to simulate the dialog but not sure how to deal with the DialogInterface.OnClickListener.
Add this to your code:
if(!((Activity) context).isFinishing())
{
//show dialog
}
While running asyntask and doing background activity gets killed and Garbage collected..Hence on completion of task you cannot show a dialog without activity.You can try either
1.By adding below permission showing your dialog .Add this permission to manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
And while showing dialog add this line of code before dialog.show()
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
2.On finsihing of the task launch the wanted activity and open application
3.Use Toast instead of dialog.
I cant get a progress dialog to show when I need it to. I have tried putting it in my asyncTask the ui class and the its own thread that runs on the ui and none have worked. Can anyone help me?
the method where the progressDialog method is called:
public void shareTest(View view){ //method called to jump to share activity if criteria matched
if(checkInputs()) { //call to check inputs
Share start = new Share();
boolean isConnected=start.connectToServer(); //connectToServer
Intent intent = new Intent(HomeScreen.this, Share.class); //create intent to move to share class from this activity
startProgressDialog();
if (isConnected) { //check to see if isconnected was succesful
if (Share.matchFound ){ //check to see if a match was found
progress.dismiss();
startActivity(intent); //if true jump to share activity
} else {
while (!Share.timedOut) { //While the time has not timedOut
if (Share.matchFound) { //if a share has been found
startActivity(intent); //start share activity
break; //if true then break
}
}
if (Share.timedOut) {
//send an notice that a match wasn't found
sendToast(getString(R.string.noShare)); //if not true then send Toast
}
}
}
else sendToast(getString(R.string.errServCon)); //if connection to server failed then send toast
}
}
this is the method:
void startProgressDialog() {
new Thread(new Runnable() {
#Override
public void run() { //creates a new runnable thread
// Issue command() on a separate thread
while (!Share.matchFound) { //while havent been asked to disconnect //if a new location has been recieved
activity.runOnUiThread(new Runnable() {
#Override
public void run() { //run on the ui thread act
progress.show(); //call the method that does the update
}
});
}
progress.dismiss();
}
}).start();
}
Declare a global variable like this:
ProgressDialog progress;
Wherever you want to show the progress, paste this code:
progress = ProgressDialog.show(this, "Please wait",
"Loading..", true);
When you are done, simply dismiss it:
progress.dismiss();