1

アプリで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>
4

1 に答える 1

1

入れてください

android:configChanges = "orientation |keyboardHidden"

向きの変更をサポートしたいアクティビティで。

于 2012-06-26T18:09:01.693 に答える