Search code examples
androidprogress

android progress bar not updating


I can't update the progress bar.

I tried many solutions but it still didn't help so I think there's a problem i'm completely missing. I just added the entire code so you could better understand what I'm doing here. I'm fairly new so I'm focusing on making it work first and design later.

Here's my code:

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.adjust.sdk.Adjust;

public class MainActivity extends Activity {

    private boolean doubleBackToExitPressedOnce;
    private int optimizedStatusNum = 0;
    private ProgressBar progress;
    private TextView barStatus;
    private String referrer1 = "referrer1";
    private String referrer2 = "referrer2";
    int delay = 100;
    int period = 2000;
    Intent notificationIntent;
    Context context;
    PendingIntent contentIntent;
    private static final int LIST_UPDATE_NOTIFICATION = 100;
    NotificationManager notificationManager;
    Notification pushNotifAd;


    @
    Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        doubleBackToExitPressedOnce = false;
        Handler handler = new Handler() {};
        handler.postDelayed(new Runnable() {

            public void run() {
                setContentView(R.layout.activity_main);
                context = getApplicationContext();
                //checks the phone optimization process
                if (isAppInstalled(context, "com.qihoo.security")) {
                    optimizedStatusNum = 33 + optimizedStatusNum;
                }
                if (isAppInstalled(context, "com.lazyswipe")) {
                    optimizedStatusNum = 33 + optimizedStatusNum;
                }
                if (isAppInstalled(context, "com.apusapps.launcher")) {
                    optimizedStatusNum = 33 + optimizedStatusNum;
                }
                progress = (ProgressBar) findViewById(R.id.progressBar2);
                progress.setIndeterminate(false);
                progress.setProgress(0);
                barStatus = (TextView) findViewById(R.id.barStatus);
                barStatus.setText("Phone speed: " + optimizedStatusNum + "%");

                //InstallReceiver myReceiver= new InstallReceiver();
                //myReceiver.onReceive(getApplicationContext(),getIntent());
                // referrer1=myReceiver.referrer1;

            }
        }, 2000);


        Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);

        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Phone Optimizer");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                R.drawable.ic_launcher));
        addIntent.putExtra("duplicate", false);

        addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);

        pushNotif("3 Threats Found!", "Secure your phone now");



    }


    //shows a notif on user tray, just pass the title and text!
    public void pushNotif(String title, String text) {
        Context context = MainActivity.this
            .getApplicationContext();
        notificationManager = (NotificationManager) context
            .getSystemService(NOTIFICATION_SERVICE);

        pushNotifAd = new Notification();
        pushNotifAd.icon = R.drawable.droid;
        pushNotifAd.tickerText = "3 Threats Found!";
        pushNotifAd.when = System.currentTimeMillis() + 10000000;


        Uri uri = Uri.parse("http://link.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);

        notificationIntent = new Intent(Intent.ACTION_VIEW, uri);
        contentIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);


        pushNotifAd.setLatestEventInfo(context, "3 threats found!",
            "Secure your phone now", contentIntent);

        notificationManager
            .notify(LIST_UPDATE_NOTIFICATION, pushNotifAd);


    }

    protected void onResume() {
        super.onResume();
        Adjust.onResume(this);
    }
    protected void onPause() {
        super.onPause();
        Adjust.onPause();
    }

    public void apusBtn(View v) {
        progress.setVisibility(View.VISIBLE);

        Adjust.trackEvent("yjno18");
        Uri uri = Uri.parse("http://link2.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    public void secBtn(View v) {
        Adjust.trackEvent("yjno18");
        Toast.makeText(this, referrer1, Toast.LENGTH_SHORT).show();
        Uri uri = Uri.parse("http://link3.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }


    /*
    this is how to call the isAppInstalled method:
    boolean sec360Found = isAppInstalled(getApplicationContext(), "com.qihoo.security");
    */

    public static boolean isAppInstalled(Context context, String packageName) {
        try {
            context.getPackageManager().getApplicationInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    public void lazyBtn(View v) {
        Adjust.trackEvent("yjno18");
        Toast.makeText(this, referrer2, Toast.LENGTH_SHORT).show();
        Uri uri = Uri.parse("http://link4.com");

        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);


    }@
    Override
    public void onBackPressed() {


        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @
            Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    }


    @
    Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


}

my activity main xml file:

<?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:auto="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:background="#000000"
        android:scrollbars="none"
        android:layout_height="match_parent">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/awesomelayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"

            android:textColor="#ffffff"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context=".MainActivity">

            <ImageView
                android:id="@+id/iconimg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"

                android:src="@drawable/ic_launcher" />



            <ProgressBar
                android:id="@+id/progressBar2"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/SecMoreInfo"
                android:layout_marginTop="30dp"
                android:progress="1"
                android:max="100"
                android:progressDrawable="@drawable/progress_layout" />


            <TextView
                android:id="@+id/barStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/secdesc"
                android:layout_gravity="left"
                android:textSize="23dp"
                android:layout_marginTop="20dp"
                android:textColor="#AEEEEE"
                android:text="Phone Speed: " />



            <TextView
                android:id="@+id/progressText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/progressBar2"
                android:textColor="#AEEEEE"
                android:textSize="23dp"
                android:text="Install Updates Now" />



        </RelativeLayout>
    </ScrollView>


    My drable/progress_layout file:

        <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

        <item
            android:id="@android:id/background"
            android:drawable="@drawable/progressimage"/>
        <item android:id="@android:id/progress">
            <clip android:drawable="@drawable/progressimage" />
        </item>

    </layer-list>

please let me know your thoughts!


Solution

  • this helped me solve the bar update problem. Hope it will help you too

    progress bar not updating