0

私はアラームアプリケーションを構築しています。ボタンをクリックすると、アプリケーションの一部がデバイスの内部および外部メモリをスキャンListViewし、システム内のすべてのオーディオ ファイルを表示します。コードの関連部分は次のとおりです。

UpdateTime.java:

public class UpdateTime extends Activity {

    Intent changeSound;
    Button alarmSound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_time);

        alarmSound = (Button)findViewById(R.id.alarmSound);
        alarmSound.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

                changeSound = new Intent (UpdateTime.this, SelectAudio.class);
                startActivityForResult(changeSound, 1);         
            }
        });
    }

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

そして、オーディオ ファイルを検索して に表示するクラスを次に示しますListActivity。このクラスのメソッドを別の場所で確認しましたがlistData()、問題なく動作しているため、必要に応じてその部分を読み飛ばしてください。

SelectAudio.class:

public class SelectAudio extends ListActivity {

    public static final int selectAudioInteger = 2;

    Intent incomingIntent;
    String fileName [];
    String fileTitle [];
    String names = "";
    String titles = "";

    ContentResolver myResolver;
    Cursor myCursor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        myResolver = this.getContentResolver();
        incomingIntent = getIntent();
        listData();
        fileName = names.split("\n");
        fileTitle = titles.split("\n");

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, fileTitle));

    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String str =  fileName[position];
        incomingIntent.putExtra("fileTitle", str);
        setResult(selectAudioInteger, incomingIntent);
        Toast toast = Toast.makeText(getApplicationContext(), fileTitle[position] + " selected" , Toast.LENGTH_SHORT);
        toast.show();
        finish();
    }

    void listData()
    {
        Uri uriInternal = android.provider.MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
        Uri uriExternal = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

        Cursor curExternal = myResolver.query(uriExternal, null, MediaStore.Audio.Media.IS_MUSIC, null, null);
        Cursor curInternal = myResolver.query(uriInternal, null, MediaStore.Audio.Media.IS_MUSIC, null, null);

        int iTitleExternal = curExternal.getColumnIndex(MediaStore.Audio.Media.TITLE);
        int iTitleInternal = curInternal.getColumnIndex(MediaStore.Audio.Media.TITLE);

        int iNameExternal = curExternal.getColumnIndex(MediaStore.Audio.Media.DATA);
        int iNameInternal = curExternal.getColumnIndex(MediaStore.Audio.Media.DATA);

        while(!curExternal.moveToLast())
        {
            names = names + curExternal.getString(iNameExternal) + "\n";
            titles = titles + curExternal.getString(iTitleExternal) + "\n";
            curExternal.moveToNext();
        }

        while (!curInternal.moveToLast())
        {
            names = names + curInternal.getString(iNameInternal) + "\n";
            titles = titles + curInternal.getString(iTitleInternal) + "\n";
            curInternal.moveToLast();
        }

    }

}

このコードの問題は、ボタンをクリックすると画面が空白になり、logcat で次のようなメッセージが表示されることです。

03-19 18:18:18.798: W/ActivityManager(59): Activity pause timeout for HistoryRecord{4a2a3d70 com.ikshvaku.intelligentalarm/.UpdateTime}
03-19 18:18:21.854: I/ActivityManager(59): Displayed activity com.ikshvaku.intelligentalarm/.SelectAudio: 3053 ms (total 3053 ms)
03-19 18:18:28.322: W/ActivityManager(59): Launch timeout has expired, giving up wake lock!
03-19 18:18:28.855: W/ActivityManager(59): Activity idle timeout for HistoryRecord{4a12ce98 com.ikshvaku.intelligentalarm/.SelectAudio}

問題の解決策は、AsyncTask クラスの使用に関連しています。問題を解決する方法を見つけることができません。

誰でも助けてください!! ありがとう。

4

1 に答える 1

0

リストアクティビティに対して onCreate が呼び出されたら...読み込みビューを配置し、非同期タスクを開いてデータを取得する必要があります...データの読み込みが最終的に完了したら..次にリストビューを更新します...ユーザーがアプリがフリーズしていないことを知る方法...これが私がやった方法です..コードの残りの部分が完全に機能すると仮定します!

public class SelectAudio extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myResolver = this.getContentResolver();
        incomingIntent = getIntent();
        fileName = names.split("\n");
        fileTitle = titles.split("\n");

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, fileTitle));
        new GetDataFromDisk().execute();
    }



    private class GetDataFromDisk extends AsyncTask<String, Void, String> {

          @Override
          protected String doInBackground(String... params) {
             listData();
             return null;
          }      

          @Override
          protected void onPostExecute(String result) {               
          }

          @Override
          protected void onPreExecute() {
            ArrayAdapter adapter = getListAdapter();
            adapter.notifyDataSetChanged();
          }
    }
}
于 2013-03-19T13:05:43.540 に答える