2016-04-10 11 views
0

私は一日中すべてを試しました。私はそれらをクリックすると、別のボタンが動作するようにしたい。私はAsyncTasksを使ってそうしていました。AsyncTaskがURIを表示しない

MainActivity.java

package com.mudd.devin.drive_now; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

import java.net.URI; 

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     setContentView(R.layout.activity_main); 

     final Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 
       new Action().execute(); 
      } 
     }); 
    } 
} 

Action.java

package com.mudd.devin.drive_now; 

import android.content.Intent; 
import android.net.Uri; 
import android.os.AsyncTask; 

import java.net.URI; 
import java.util.Locale; 

/** 
* Created by Devin on 4/9/2016. 
*/ 
public class Action extends AsyncTask<URI, URI, Intent> { 




    @Override 
    protected Intent doInBackground(URI... params) { 
     String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps"); 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
     intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
     return intent; 
    } 


} 

事前にありがとうございます。今日、私は本当にたわごとの開発者であり、アプリを作るのは難しいことに気付きました。

+0

あなたは実際に 'onPostExecute(Intent result)'で何かをしていますか? 'Action''AsyncTask'でこのメソッドをオーバーライドしてインテントを使って何かをしましたか?またこれを見てください:https://developers.google.com/maps/documentation/android-api/intents#overview P.S.あまりにも自分自身では難しくありません! –

+0

私はonPostExecuteで何もしていませんでした。私はdoInBackgroundに触れていました。 Googleマップのコードは以前から使用していたので動作することがわかりました。 – deeup511

+0

目的で何かをするには、このメソッドをオーバーライドする必要があります。 Android Studioを使用している場合は、Actionクラスのどこかにカーソルを置いて、「コード」メニューに移動して「メソッドをオーバーライド」を選択し、onPostExecute()を選択します。これにより、あなたは 'startActivity(intent) 'のような結果オブジェクト(インテント)で何でもできます。正直言って、あなたがやっていることのために、この操作のためにAsyncTaskは必要ありません。 –

答えて

0

まず、asynctaskを使用して別のプロセスをインテントで起動したいのはなぜですか? AyncTaskは、ネットワーク操作やファイル読み込みのような強力な処理に便利ですが、意図を作成するためにそれを使用するのは本当に無駄です!

そして、AsyncTaskにURIパラメータを渡すことができません。

あなたはonclickから直接インテントを呼び出すことができます(私がお勧めします)。それとも、どこかにこれらなどから活動を開始する必要があります:

方法1


public class Action extends AsyncTask<URI, Void, Intent> { 
    private final Context context; 
    public Action(Context context){ 
     this.context = context; 
    } 

    @Override 
    protected Intent doInBackground(URI... params) { 
     String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps"); 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
     intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
     return intent; 
    } 

    @Override 
    protected void onPostExecute(Intent intent){ 
     context.startActivity(intent); 
    } 
} 

そしてこの

new Action(context).execute(); 

方法2のようにそれを呼び出す:


public class Action extends AsyncTask<Void, Void,Void>{ private final Context context; 
    public Action(Context context){ 
     this.context = context; 
    } 

    @Override 
    protected Intent doInBackground(Void params) { 
     String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps"); 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
     intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
     context.startActivity(intent) 


    return null; 
} 
} 

方法

button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 
      Uri uri = Uri.parse("http://maps.google.com/maps"); 
      Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
      intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
      startActivity(intent) 
      } 
     }); 
推奨そして、私はあなたがクラス名としてアクションを使用することはお勧めしません!

+0

これは以前の私のやり方とまったく同じです。どうもありがとうございます。 – deeup511

+0

@ deeup511if助けてくれて、plsが私の答えを受け入れる –

0

onPostExecuteAsyncTaskに追加する必要があります。そうしないと、結果(意図)が破棄されます。

など。

public class Action extends AsyncTask<URI, Void, Intent> { 
    private final Context context; 
    public Action(Context context){ 
     this.context = context; 
    } 

    @Override 
    protected Intent doInBackground(URI... params) { 
     String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps"); 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
     intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
     return intent; 
    } 

    @Override 
    protected void onPostExecute(Intent intent){ 
     context.startActivity(intent); 
    } 
} 
関連する問題