我在我的应用程序中使用AsyncTask。
即使方向发生变化,如何使我的任务恢复正常工作。
这项任务: -
package com.android.grad;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
public class LoginTask extends AsyncTask<Void, Void, Boolean> {
private Activity activity;
private ProgressDialog pd;
private Uri fileUri;
private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public LoginTask(Activity activity, Uri fileUri) {
this.activity = activity;
this.fileUri = fileUri;
}
@Override
protected void onPreExecute() {
pd = ProgressDialog.show(activity, "Signing in",
"Please wait while we are signing you in..");
}
@Override
protected Boolean doInBackground(Void... arg0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
pd.dismiss();
pd = null;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
activity.startActivityForResult(intent,
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
public class LoginActivity extends Activity {
private final int MEDIA_TYPE_IMAGE = 1;
private CheckBox rememberMe;
private EditText userName, passWord;
private Button loginBtn;
private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private String path;
private Uri fileUri;
private OnClickListener loginOnClick = new OnClickListener() {
public void onClick(View v) {
new LoginTask(LoginActivity.this, fileUri).execute();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
path = fileUri.getPath();
loginBtn = (Button) findViewById(R.id.signInBtn);
loginBtn.setOnClickListener(loginOnClick);
rememberMe = (CheckBox) findViewById(R.id.keepMe);
userName = (EditText) findViewById(R.id.userNameID);
passWord = (EditText) findViewById(R.id.passwordID);
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
try {
userName.setText(sp.getString("username", ""));
passWord.setText(sp.getString("password", ""));
rememberMe.setChecked(sp.getBoolean("rememberMe", false));
} catch (ClassCastException ex) {
}
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
Editor editor = sp.edit();
if (rememberMe.isChecked()) {
editor.putString("username", userName.getText().toString());
editor.putString("password", passWord.getText().toString());
} else {
editor.putString("username", "");
editor.putString("password", "");
}
editor.putBoolean("rememberMe", rememberMe.isChecked());
editor.commit();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Intent intent = new Intent(LoginActivity.this,
PreviewActivity.class);
intent.putExtra("path", path);
startActivity(intent);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
Toast.makeText(getApplicationContext(),
"User cancelled the image capture", Toast.LENGTH_LONG)
.show();
} else {
// Image capture failed, advise user
}
}
}
我如何在LoginActivity中处理它。 如何在屏幕方向后保存progressBar的状态继续。
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Camera"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-configuration android:configChanges="orientation|keyboardHidden|navigation"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:debuggable="true">
<activity
android:name="com.android.grad.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.android.grad.CameraActivity" >
</activity>
<activity
android:name="com.android.grad.LoginActivity"
android:configChanges="orientation|keyboardHidden" >
</activity>
<activity
android:name="com.android.grad.PreviewActivity"
>
</activity>
<activity android:name="com.android.grad.BuiltInCamera"></activity>
</application>
</manifest>
答案 0 :(得分:1)
请放
机器人:configChanges = “取向| keyboardHidden”
在您希望为方向更改提供支持的活动中。