2

Ich erstelle also eine App, mit der der Benutzer entscheiden kann, ob er WiFi oder 4G zum Abrufen der Daten nach dem Anmelden verwenden möchte (ich werde eine weitere Frage zur Implementierung des Entscheidung später, es sei denn, jemand möchte dies auch hier hinzufügen). Zurzeit habe ich eine MainActivity.java, die die Anmeldung übernimmt (die ich noch implementieren muss) und ich habe eine NavDrawerActivity.java für die Navigationsschublade. Jetzt, nachdem ich den Log-in gemacht habe, geht es natürlich zum Nav-Drawer. Ich möchte jedoch, dass der Benutzer wählen kann, ob er Wifi oder Daten vor der Anmeldung verwenden möchte oder nicht, und ob er nach der Anmeldung wählen kann. Ich denke, das ist eine zweiteilige Frage, wie würdest du das umsetzen? gut, ist es eine gute Idee, das in den Login-Bildschirm zu setzen? Oder sollte ich es nur standardmäßig verwenden, um WLAN immer zu verwenden, wenn sie es nicht ändern, und wenn ich WiFi nicht aktiviert erkenne, haben sie es aktivieren?Verwenden der Nav - Drawer - Einstellungen in verschiedenen Aktivitäten

MainActivity.java Code

package com.example.jamessingleton.chffrapi; 

import android.content.Context; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.TextView; 


import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import org.json.JSONTokener; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 


public class MainActivity extends AppCompatActivity { 

    EditText emailText; 
    TextView responseView; 
    ProgressBar progressBar; 
    static final String API_KEY = "USE_YOUR_OWN_API_KEY"; 
    static final String API_URL = "https://api.fullcontact.com/v2/person.json?"; 
    static final String ClientId= ""; 
    static final String ClientSecret = ""; 



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

     responseView = (TextView) findViewById(R.id.responseView); 
     emailText = (EditText) findViewById(R.id.emailText); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar); 
     final Context context = this; 
     Button queryButton = (Button) findViewById(R.id.queryButton); 
     queryButton.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View v) { 
       new RetrieveFeedTask().execute(); 

       Intent intent = new Intent(context, NavDrawerActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 


    class RetrieveFeedTask extends AsyncTask<Void, Void, String> { 

     private Exception exception; 

     protected void onPreExecute() { 
      progressBar.setVisibility(View.VISIBLE); 
      responseView.setText(""); 
     } 

     protected String doInBackground(Void... urls) { 
      String email = emailText.getText().toString(); 
      // Do some validation here 

      try { 
       URL url = new URL(API_URL + "email=" + email + "&apiKey=" + API_KEY); 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
       try { 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
        StringBuilder stringBuilder = new StringBuilder(); 
        String line; 
        while ((line = bufferedReader.readLine()) != null) { 
         stringBuilder.append(line).append("\n"); 
        } 
        bufferedReader.close(); 
        return stringBuilder.toString(); 
       } 
       finally{ 
        urlConnection.disconnect(); 
       } 
      } 
      catch(Exception e) { 
       Log.e("ERROR", e.getMessage(), e); 
       return null; 
      } 
     } 

     protected void onPostExecute(String response) { 
      if(response == null) { 
       response = "THERE WAS AN ERROR"; 
      } 
      progressBar.setVisibility(View.GONE); 
      Log.i("INFO", response); 
      responseView.setText(response); 
      // TODO: check this.exception 
      // TODO: do something with the feed 

//   try { 
//    JSONObject object = (JSONObject) new JSONTokener(response).nextValue(); 
//    String requestID = object.getString("requestId"); 
//    int likelihood = object.getInt("likelihood"); 
//    JSONArray photos = object.getJSONArray("photos"); 
//    . 
//    . 
//    . 
//    . 
//   } catch (JSONException e) { 
//    e.printStackTrace(); 
//   } 
     } 
    } 
} 

NavDrawerActivity.java Code

package com.example.jamessingleton.chffrapi; 

import android.app.FragmentManager; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 




public class NavDrawerActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener{ 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_nav_drawer); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 

      fab.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        //.setAction("Action", null).show(); 
        Intent sendIntent = new Intent(); 
        sendIntent.setAction(Intent.ACTION_SEND); 
        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); 
        sendIntent.setType("text/plain"); 
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 
       } 
      }); 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 


    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     FragmentManager fragmentSettingsManager = getFragmentManager(); 
     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      fragmentSettingsManager.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit(); 
      setTitle(R.string.action_settings); 
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
      fab.setVisibility(View.GONE); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
     FragmentManager fragmentManager = getFragmentManager(); 
     if (id == R.id.nav_first_layout) { 
      fragmentManager.beginTransaction().replace(R.id.content_frame, new FirstFragment()).commit(); 
      setTitle(R.string.speed_graph); 
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
      fab.setVisibility(View.VISIBLE); 
     } else if (id == R.id.nav_second_layout) { 
      fragmentManager.beginTransaction().replace(R.id.content_frame, new SecondFragment()).commit(); 
      setTitle(R.string.drive_player); 
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
      fab.setVisibility(View.VISIBLE); 
     } else if (id == R.id.nav_third_layout) { 
      fragmentManager.beginTransaction().replace(R.id.content_frame, new ThirdFragment()).commit(); 
      setTitle(R.string.google_maps); 
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
      fab.setVisibility(View.VISIBLE); 
     } else if (id == R.id.nav_share) { 
      Intent sendIntent = new Intent(); 
      sendIntent.setAction(Intent.ACTION_SEND); 
      sendIntent.putExtra(Intent.EXTRA_TEXT, "Go Check Out All Driving Data in the Play Store!"); 
      sendIntent.setType("text/plain"); 
      startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

Lassen Sie mich wissen, ob es etwas ist, was Sie brauchen Jungs und danke euch für all die Hilfe :)

Hier ist der WifivsDataDialog Code

package com.example.jamessingleton.chffrapi; 

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.content.DialogInterface; 
import android.os.Bundle; 

/** 
* Created by James Singleton on 8/9/2016. 
*/ 

public class WifivsDataDialog extends DialogFragment { 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the Builder class for convenient dialog construction 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setMessage(R.string.dialog_box) 
       .setPositiveButton(R.string.WiFi, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // FIRE ZE MISSILES! 
        } 
       }) 
       .setNegativeButton(R.string.Cell_Data, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled the dialog 
        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
    } 
} 

Antwort

1

Es ist nicht ganz klar, was Sie erreichen möchten.

Muss die Daten abgerufen werden? Was passiert, wenn der Benutzer sowohl WLAN als auch das Mobilfunknetz auf dem Gerät deaktiviert hat? Was passiert, wenn sie sich für die Nutzung von WLAN entscheiden, aber nur das Mobilfunknetz auf dem Gerät aktiviert ist?

Ich würde eine Dialog am Anfang der App erstellen, die dieses Zeug erklärt und lassen Sie den Benutzer wählen, ob sie Daten im Mobilfunknetz oder nur WLAN mit einer abrufen möchten "Zeigen Sie dies nicht Dialog erneut "CheckBox.

Wenn der Benutzer die Option CheckBox aktiviert wählt, ist dies das Standardverhalten bei weiteren Starts (und kann in den Einstellungen/Einstellungen geändert werden).

Sie können die Dialog aus der onCreate() Ihres Login Activity zeigen.

Zum Beispiel:

connection_dialog.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Would you like to allow fetching data on mobile network?"/> 

    <RadioGroup 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Yes"/> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="No, allow Wi-Fi only"/> 
    </RadioGroup> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:text="OK"/> 

</LinearLayout> 

ConnectionDialogFragment.java:

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.DialogFragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class ConnectionDialogFragment extends DialogFragment { 

    public ConnectionDialogFragment() { 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.connection_dialog, container, false); 

     // set up your View here 

     return view; 
    } 
} 

Und in Ihrem Activity:

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

    // ... 

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    if(prefs.getBoolean("show_dialog", true)) { 
     showDialog(); 
    } 
} 

private void showDialog() { 
    ConnectionDialogFragment dialog = new ConnectionDialogFragment(); 
    dialog.show(getSupportFragmentManager(), "connection_dialog"); 
} 

Schauen Sie sich die Entwickleranleitung unter Dialogs an.

Wenn der Benutzer hat sich dafür entschieden, ob sie wollen nur Wi-Fi nutzen oder erlauben auch Mobilfunknetz, können Sie die verfügbare Verbindung mit so etwas wie dies überprüfen konnte: die nach

// this method will return either 
// ConnectivityManager.TYPE_MOBILE 
// or 
// ConnectivityManager.TYPE_WIFI 
// or -1 (if no connection is available) 
private int checkAvailableConnectionType() { 
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetwork = manager.getActiveNetworkInfo(); 
    int type = activeNetwork.getType(); 
    if(activeNetwork.isConnected() && (type == ConnectivityManager.TYPE_MOBILE || 
      type == ConnectivityManager.TYPE_WIFI)) { 
     return type; 
    } 
    return -1; 
} 

Und tun Sie Ihre Sachen Wahl des Benutzers und der derzeit aktiven Verbindung (raten Sie dem Benutzer zum Beispiel, Wi-Fi einzuschalten).

Verwandte Themen