onPostExecute() not executing - android

I want to make switch from one activity to another when the progress bar reached to maximum.
Activity A
public class A extends Activity {
private ProgressBar progressBar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.mainpage);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.main);
progressBar = (ProgressBar) findViewById(R.id.prog_bar);
new AsyncTask<Void, Integer, Integer>(){
#Override
protected Integer doInBackground(Void... params) {
int progressStatus = 0;
while (progressStatus < 5000) {
progressStatus++;
publishProgress(progressStatus);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return 1;
}
#Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
}
protected void onPostExecute(Integer result) {
final Intent mainIntent = new Intent(A.this,B.class);
startActivity(mainIntent);
finish();
return;
}
}.execute();
}
}
But in this code the onPostExecute() method is not execting...I got stuck with the parameters of aSyncTask also...
When I run this code in debug mode it returns from try block
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
XML for this class
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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:paddingTop="5dp"
android:background="#drawable/eggstation"
tools:context=".A" >
<TextView
android:id="#+id/dutech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-5dp"
android:text="#string/text_data"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#00cccc"
android:textSize="25sp" />
<TextView
android:id="#+id/devby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/dutech"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="Developed by:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#3399cc"
android:textSize="28sp" />
<ProgressBar
android:id="#+id/prog_bar"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:max="100"
android:layout_alignParentTop="true" />

your AsyncTask code has no problem !
problem may be with starting Activity
so try following code in onPostExecute()
final Intent mainIntent = new Intent(A.this,B.class);
startActivity(mainIntent);
finish();
return;

Try the following code snippet
protected void onPostExecute(Integer result) {
startActivity(new Intent(A.this,B.class));
return;
}
Also sleep method take long type as argument, try the following
Thread.sleep(1000);
Also register your B activity in Manifest.xml like the follwing
<application>
...
...
<activity
android:name="[package_name].B"
android:label="#string/class_b" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="[package_name].MainActivity" />
</activity>
</application

Related

How can i show ProgressBar while MediaPlayer is preparing to play?

I want to show a ProgressBar while MediaPlayer is preparing to play and when MediaPlayer starts playing ProgressBar disappear .
this is my code
public class MainActivity extends Activity {
String path="http://live.mp3quran.net:8006/;";
Button play_button, pause_button;
MediaPlayer player =new MediaPlayer();
private ProgressBar mProgress;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play_button=(Button)findViewById(R.id.play_button);
pause_button=(Button)findViewById(R.id.pause_button);
mProgress=(ProgressBar)findViewById(R.id.progressBar);
mProgress.setVisibility(View.INVISIBLE);
play_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mProgress.setVisibility(View.VISIBLE);
try {
player.setDataSource(path);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(mProgress.getVisibility()==View.VISIBLE)
mProgress.setVisibility(View.INVISIBLE);
}
}
});
pause_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
player.reset();
}
});
}
}
my XML file Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:paddingTop="#dimen/activity_vertical_margin">
<Button android:id="#+id/play_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_marginTop="60px"
android:gravity="center"
android:text="Play" />
<Button android:id="#+id/pause_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_alignParentRight="true"
android:layout_marginTop="60px"
android:text="Pause" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignBottom="#+id/text_shown"
android:layout_toRightOf="#+id/play_button"
android:layout_toLeftOf="#+id/pause_button"
android:max="100"
android:progress="20"
android:layout_toStartOf="#+id/pause_button" />
</RelativeLayout>
My problem is that ProgressBar doesn't appear.
I have tried this solution
How can i show a ProgressBar when mediaPlayer is prepairing to play
but it doesn't help.
You can use like this
final ProgressDialog ringProgressDialog = ProgressDialog.show(this, "Please wait ...", "Fetching your contact..", true);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
#Override
public void run() {
try {
// Here you should write your time consuming task...
// Let the progress ring for 10 seconds...
readContact();
// Thread.sleep(300);
} catch (Exception e) {
}
ringProgressDialog.dismiss();
}
}).start();

Manage Circular SeekBar throughout the app

I am working on an app that is used to stream songs online. I am able to play the song on clicking on it.
The problem with me is that I have a CircularSeekBar in all the Activities and I need to manage it on all the screens. If a song is played from any activity CircularSeekBar should be visible on all the screens. What I am able to do is When a song is played I am able to show the seekbar on that particular activity but not able to manage that seekbar on any other activity.
Below is the sample of one of my xml files:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
</LinearLayout>
<include layout="#layout/seek_bar_layout"></include>
</android.support.design.widget.CoordinatorLayout>
Please let me know how to manage that Seekbar on all the Activities.
Thanks in Advance.
You can't manage activity view when other is visible.
You can solve it by any boolean flag (for example: isSongPlaying = false). When you start playing your song set it to true. In each activity in onResume check that flag and show / hide your SeekBar.
Try to avoid using public static and use any other way.
EDIT:
According to your comments i created very simple example how you can solve your problem using service. Remember this code need much improvements.
SongService
public class SongService extends Service {
ActivityManager activityManager;
IBinder iBinder;
boolean playingSong = false;
Worker worker;
public SongService() {
iBinder = new MyBinder();
}
private void playSong(String path) {
if(worker != null) {
worker.cancel(true);
}
worker = new Worker();
worker.execute();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return iBinder;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
private class Worker extends AsyncTask<Void, Integer, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
playingSong = true;
}
#Override
protected Void doInBackground(Void... params) {
for (int i = 100; i > -1; i--) {
publishProgress(i);
try {
Thread.sleep(100l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (activityManager != null) {
activityManager.publishProgress(values[0]);
}
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
playingSong = false;
activityManager.onSongFinish();
}
}
public class MyBinder extends Binder {
public SongService getSongService() {
return SongService.this;
}
public void BindView(ActivityManager activityManager) {
SongService.this.activityManager = activityManager;
}
public void playSong(String path) {
SongService.this.playSong(path);
}
public boolean isPlayingSong() {
return playingSong;
}
}
public interface ActivityManager {
void publishProgress(int progress);
void onSongFinish();
}
}
MainActivity:
public class MainActivity
extends AppCompatActivity
implements ServiceConnection, SongService.ActivityManager {
TextView progress;
Button play;
Button next;
SongService.MyBinder myBinder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = (TextView) findViewById(R.id.progress);
play = (Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(myBinder != null) {
//Your path to song
myBinder.playSong("");
play.setEnabled(false);
}
}
});
next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
#Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, SongService.class);
bindService(mIntent, this, BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
unbindService(this);
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
if(service instanceof SongService.MyBinder) {
myBinder = (SongService.MyBinder) service;
myBinder.BindView(this);
if (myBinder.isPlayingSong()) {
play.setEnabled(false);
}
else {
play.setEnabled(true);
}
}
}
#Override
public void onServiceDisconnected(ComponentName name) {
myBinder = null;
}
#Override
public void publishProgress(int progress) {
this.progress.setText("Progress: " + progress);
}
#Override
public void onSongFinish() {
play.setEnabled(true);
}
}
MainActivity2
This activity class looks exacly the same like MainActivity but have different layout file.
MainActivity - Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.aszymanski2.serviceanddelegatepatternexample.MainActivity">
<TextView
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Play"
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<Button
android:text="SecondActivity"
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
MainActivity2 - Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.aszymanski2.serviceanddelegatepatternexample.MainActivity">
<TextView
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Play"
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<Button
android:text="Back"
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>

How to make 'if I press the button, Download Manager turn on'?

I will make rooting Tool but I want 'if I press the button, Download Manager turn on' code. Please tell me
and I'm going to start developing with the help ..
public class MainActivity extends Activity
{
DownLoadComplte mDownload;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startDownload()
{
DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Request mRqRequest = new Request(Uri.parse("http://androidtrainningcenter.blogspot.in/2012/11/android-webview-loading-custom-html-and.html"));
mRqRequest.setDescription("This is File");
mRqRequest.setDestinationUri(Uri.parse("/sdcard/download"));
long idDownLoad=mManager.enqueue(mRqRequest);
}
#Override
protected void onStart() {
super.onStart();
mDownload = new DownLoadComplte();
registerReceiver(mDownload, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mDownload);
}
private class DownLoadComplte extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(
DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
Toast.makeText(context, "Download Complte", Toast.LENGTH_LONG)
.show();
}
}
}}
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Start Download"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
</LinearLayout>
Thank you for any help & really appreciate
Replace your code with this:
Java Code:
public void startDownload(View v)
{//do something..
}
XML Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Start Download"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startDownload"/> // Change this
</LinearLayout>

How to handle the onResume in web service call

I am working on Payment based application. so in this case in first screen user has to selectproduct items and moves to next screen, in screen 2 he has to choose the payment method either
cash or card. in screen one the store names are displaying from web service. i have handled that
in A sync Task.if he choose cash mode from screen 2, the payment will be get authorized and
displays the respective message.
But in case if he wants to go back the product screen(screen 1) from the screen 2,screen 1 will
not call the web service again. it should be as the same way . but if the payment is completed it
will be refreshed. how do i achieve this? Do i mention the whole operation in on Resume method.
Check this below example by Commonsware, hope this helps you.
public class RotationAsync extends Activity {
private ProgressBar bar=null;
RotationAwareTask task=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bar=(ProgressBar)findViewById(R.id.progress);
Button next = (Button) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent (RotationAsync.this, First.class);
startActivity(i);
}
});
task=(RotationAwareTask)getLastNonConfigurationInstance();
if (task==null) {
task=new RotationAwareTask(this);
task.execute();
}
else {
task.attach(this);
updateProgress(task.getProgress());
if (task.getProgress()>=100) {
markAsDone();
}
}
}
#Override
public Object onRetainNonConfigurationInstance() {
task.detach();
return(task);
}
void updateProgress(int progress) {
bar.setProgress(progress);
}
void markAsDone() {
findViewById(R.id.completed).setVisibility(View.VISIBLE);
}
static class RotationAwareTask extends AsyncTask<Void, Void, Void> {
RotationAsync activity=null;
int progress=0;
RotationAwareTask(RotationAsync activity) {
attach(activity);
}
#Override
protected Void doInBackground(Void... unused) {
for (int i=0;i<20;i++) {
SystemClock.sleep(500);
publishProgress();
}
return(null);
}
#Override
protected void onProgressUpdate(Void... unused) {
if (activity==null) {
Log.w("RotationAsync", "onProgressUpdate() skipped -- no activity");
}
else {
progress+=5;
activity.updateProgress(progress);
}
}
#Override
protected void onPostExecute(Void unused) {
if (activity==null) {
Log.w("RotationAsync", "onPostExecute() skipped -- no activity");
}
else {
activity.markAsDone();
}
}
void detach() {
activity=null;
}
void attach(RotationAsync activity) {
this.activity=activity;
}
int getProgress() {
return(progress);
}
}
}
First.java
public class First extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ProgressBar android:id="#+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="#+id/completed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Work completed!"
android:visibility="invisible"
/>
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>

Android layout not loaded on onStart()

While showing a background I want to run some operations (without using threads).
In my activity on onCreate() I have setContentView(R.layout.splash);
When the app fires onStart(), the layout is not loaded yet. Why?
UPDATE: LogCat gives me "Activity has leaked window"
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="#drawable/splash"
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" />
</LinearLayout>
onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
onStart
#Override
protected void onStart() {
super.onStart();
if (CBConstants.ASSETS_MANAGER){
String progressMsg = this.getResources().getString(R.string.progress_dialog_assets_upgrading_message);
ProgressDialog progressDialog = ProgressDialog.show(this, null, progressMsg);
progressDialog.show();
try {
[...]
} catch(Exception e) {
Log.d("appcheck", ""+e);
Intent intent = new Intent(SplashScreen.this, CBWebViewActivity.class);
startActivity(intent);
}
}
else {
Thread noAssetManager = new Thread() {
public void run() {
try {
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {
if (LOG) Log.d("appcheck", ""+e);
}
finally {
Intent intent = new Intent(SplashScreen.this, CBWebViewActivity.class);
startActivity(intent);
}
}
};
noAssetManager.start();
}
}

Categories

Resources