所以我正在创建一个应用程序,当你第一次打开它时,你可以选择使用WiFi或数据来检索oAuth2的数据(仍然需要弄明白)。但是,我的导航抽屉中还有一个设置按钮,其中包含一个启用或禁用使用数据的无线电组。如果该人说要启用数据,我关闭他们的WiFi(我不应该这样做)我应该检查连接状态,每当连接等于移动数据然后开始上传,如果他们点击禁用检查连接是什么时候WiFi并开始上传。但是,我现在不知道该怎么做,除非现在比我聪明的人能让我知道,就是这样。
但是,我在保存按钮状态时按下了保存按钮,然后在打开应用程序时加载该状态/决定。此外,默认情况下它被设置为禁用,但是,如果用户在应用程序启动弹出窗口中单击“使用单元格”,我想更改该值。
我真的更喜欢学习如何做这些东西的帮助,所以非常感谢任何帮助。
这是我的MainActivity.java
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 = "";
static final String SCOPE = "";
@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);
}
});
WifivsDataDialog myDiag = new WifivsDataDialog();
//myDiag.show(getFragmentManager(), "dialog_layout");
myDiag.setCancelable(false);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
boolean dontShowDialog = sharedPref.getBoolean("DONT_SHOW_DIALOG", false);
if (!dontShowDialog) {
new WifivsDataDialog().show(getFragmentManager(), "WiFi");
}
}
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;
}
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();
// }
}
}
}
以下是控制应用启动对话框的代码。
public class WifivsDataDialog extends DialogFragment implements View.OnClickListener {
private CheckBox checkBox;
private Button button1;
private Button button2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.dialog_box_layout, container);
checkBox = (CheckBox) mainView.findViewById(R.id.checkBox);
button1 = (Button) mainView.findViewById(R.id.button1);
button2 = (Button) mainView.findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// Store the isChecked to Preference here
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("DONT_SHOW_DIALOG", isChecked);
editor.commit();
}
});
return mainView;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// Do wifi stuff here
WifiManager wifi = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
dismiss();
break;
case R.id.button2:
// Do cellular stuff here
ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
Boolean isConnected = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
if(isConnected == true)
{
dismiss();
}else
{
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Mobile Data is not turned on. Please turn Mobile Data on to continue.");
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss();
Intent intentSettings = new Intent(Settings.ACTION_SETTINGS);
startActivity(intentSettings);
}
});
alertDialog.show();
}
break;
}
}
}
这是我的代码settingsfragment.java
public class SettingsFragment extends Fragment
{
View myView;
Button button;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.settings_layout, container, false);
button= (Button)getActivity().findViewById(R.id.button3);
RadioGroup radioGroup = (RadioGroup) myView .findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId) {
case R.id.radioButton7:
// switch to fragment 1
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
Boolean isConnected = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
if(isConnected != true)
{
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Mobile Data is not turned on. Please turn Mobile Data on to continue.");
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss();
Intent intentSettings = new Intent(Settings.ACTION_SETTINGS);
startActivity(intentSettings);
}
});
alertDialog.show();
}
break;
case R.id.radioButton6:
// Fragment 2
WifiManager wifiManager1 = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager1.setWifiEnabled(true);
break;
}
}
public void onClick(View v) {
private void saveGenderInPreference () {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
RadioGroup radioGroup1 = (RadioGroup) getActivity().findViewById(R.id.radioGroup);
int selectedId = radioGroup1.getCheckedRadioButtonId();
if (selectedId == R.id.radioButton7)
editor.putBoolean("is_Wifi", true);
else
editor.putBoolean("is_Wifi", false);
editor.commit();
}
}
});
return myView;
}
}