0

私は比較的新しい Android 開発者で、現在、最初の Android アプリを完成させています。

このアプリは Web アプリの「シェル」アプリであり、フラグメントを使用していますが、2 つの問題があります。私は広範囲にわたる調査を行いましたが、うまくいくとわかったアイデアを得ることができなかったので、ここでいくつかの答えが得られることを願っています. 前もって感謝します!

1) ユーザーがデバイスの戻るボタンを使用して Web ビューに戻ることができるようにしたい

2) クラス内のメソッドから GPS の緯度と経度を、変数 myLongitude と myLatitude から渡そうとしています。

MainActivity のコードは次のとおりです。

public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Without this, location is not fetched
    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    //mlocManager.removeUpdates(mlocListener); // This needs to stop getting the location data and save the battery power.

    // Set up the action bar to show tabs.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // For each of the sections in the app, add a tab to the action bar.
    actionBar.addTab(actionBar.newTab().setText("Browse").setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("My City").setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("Search").setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("Favs").setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("Help").setTabListener(this));
}


// The serialization (saved instance state) Bundle key representing the current tab position.
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";


@Override
public void onRestoreInstanceState(Bundle savedInstanceState) 
{
    // Restore the previously serialized current tab position.
    if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) 
    {
        getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
    }
}


@Override
public void onSaveInstanceState(Bundle outState)
{
    // Serialize the current tab position.
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
}


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


//Gets the Device ID
public String getDeviceId()
{
    final String androidId, deviceId;
    androidId = android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
    deviceId = androidId.toString();

    return deviceId;
}


public class MyLocationListener implements LocationListener
{
    Double myLatitude;  //This is passing a NULL value down to onTabSelected because it is not getting a value from onLocationChanged
    Double myLongitude; //This is passing a NULL value down to onTabSelected because it is not getting a value from onLocationChanged


    @Override
    public void onLocationChanged(Location loc)
    {
        myLatitude = loc.getLatitude();
        myLongitude = loc.getLongitude();

        String Text = "My current location is: " + "Latitude = " + myLatitude + "Longitude = " + myLongitude;
        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
    }

    @Override
    public void onProviderEnabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();  
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {}

}


// When the given tab is selected, assign specific content to be displayed //
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
    Fragment fragment = new SectionFragment();
    Bundle args = new Bundle();

    final String deviceId = getDeviceId();


    MyLocationListener location = new MyLocationListener();

    final Double myLatitude = location.myLatitude; //This is returning a NULL value
    final Double myLongitude = location.myLongitude; //This is returning a NULL value   


    //Assigns a specific URL to "ARG_SECTION_URL" for each tab
    if(tab.getPosition()==0)
    {
        args.putString(SectionFragment.ARG_SECTION_URL, "http://www.myurl.com/countries.asp?Country=&State=&City=&Category=&Latitude=&Longitude=&ListingID=&AppId=aDG&DeviceID=" + deviceId + "&OrderBy=Name");         
    }
    else if(tab.getPosition()==1)
    {
        args.putString(SectionFragment.ARG_SECTION_URL, "http://www.myurl.com/landing.asp?Country=&State=&City=&Category=&Latitude=" + myLatitude + "&Longitude=" + myLongitude + "&ListingID=&AppId=aDG&DeviceID=" + deviceId + "&OrderBy=Name");          
    }
    else if(tab.getPosition()==2)
    {
        args.putString(SectionFragment.ARG_SECTION_URL, "http://www.myurl.com/searchform.asp?Latitude=&Longitude=&ListingID=&AppId=aDG&DeviceID=" + deviceId);
    }
    else if(tab.getPosition()==3)
    {
        args.putString(SectionFragment.ARG_SECTION_URL, "http://www.myurl.com/favorites.asp?Latitude=&Longitude=&ListingID=&AppId=aDG&DeviceID=" + deviceId + "&OrderBy=Name");
    }
    else if(tab.getPosition()==4)
    {
        args.putString(SectionFragment.ARG_SECTION_URL, "http://www.myurl.com/help.asp?Latitude=&Longitude=&ListingID=&AppId=aDG&DeviceID=" + deviceId);
    }

    fragment.setArguments(args);
    getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();  
}


@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
{}


@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) 
{}


@Override
public void onBackPressed() 
{

}


//A fragment representing a section of the app, but that simply displays content.
public static class SectionFragment extends Fragment 
{
    //The fragment argument representing the section number for this fragment.
    public static final String ARG_SECTION_URL = "section_url";

    public SectionFragment() 
    {}

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {        
        //Create a new WebView and set its URL to the fragment's argument value.
        WebView myWebView = new WebView(getActivity());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl(getArguments().getString(ARG_SECTION_URL));
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.getSettings().setAppCacheEnabled(true);
        myWebView.getSettings().setDatabaseEnabled(true);
        myWebView.getSettings().setDomStorageEnabled(true);    
        return myWebView;
    }   


    private class MyWebViewClient extends WebViewClient 
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            view.loadUrl(url);
            return true;
        }

    }

}

}
4

3 に答える 3

1

この方法はもっと簡単だと思います。

ではWebViewActivity.java、1 つのメソッドを追加しました。

@Override
public void onBackPressed() {

    WebViewFragment fragment = (WebViewFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragmentContainer);
    if (fragment.canGoBack()) {
        fragment.goBack();
    } else {
        super.onBackPressed();
    }
}

ではWebViewFragment.java、2 つのメソッドを追加しました。

public boolean canGoBack() {
    return mWebView.canGoBack();
}

public void goBack() {
    mWebView.goBack();
}
于 2014-10-19T10:10:45.277 に答える