我无法更新进度条。
我尝试了很多解决方案,但它仍然没有帮助,所以我认为这是一个我完全失踪的问题。我刚刚添加了整个代码,以便您更好地了解我在这里所做的事情。我很新,所以我专注于让它先工作并稍后设计。
这是我的代码:
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;
}
}
我的活动主xml文件:
<?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>
请让我知道你的想法!
答案 0 :(得分:0)
查看this,了解如何在工作线程中正确更新ProgressBar。
在run()方法内仅按setProgress(mProgressStatus);
run()方法中唯一的代码是
setContentView(R.layout.activity_main);
这与你想要实现的内容有些无关,而你在run()方法之外还有其他一切。 ProgressBar的属性定义为XML布局文件,因此您无需setMax()
。
答案 1 :(得分:0)
首先是像这样的drawable / progress_layout
<?xml version="1.0" encoding="utf-8"?>
<selector 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>
然后
Handler handler = new Handler() {};
handler.postDelayed(new Runnable() {
public void run() {
ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar2);
progress.setIndeterminate(false);
progress.setMax(100);
progress.setProgress(20);
progress.setMax(50);
progress.setProgress(50);
setContentView(R.layout.activity_main);
}
} , 2000);
有很多语法问题
答案 2 :(得分:0)