Android 系统必看小程序:代码分享

Android 系统是目前最流行的移动操作系统之一,它的开放性和灵活性使得开发者可以轻松地为其开发出各种应用程序。在这些应用程序中,小程序是一个非常有用的工具,它可以帮助用户完成许多简单的任务,而不必打开大型应用程序。

以下是几个必看的 Android 小程序,它们提供了一些代码示例,可以帮助您更好地了解 Android 开发的基本知识。

1. Hello World

这是一个非常简单的小程序,它的作用是显示一个“Hello World”文本。这个程序非常适合初学者,因为它很容易理解,并且可以用来熟悉 Android 开发的基本概念和代码结构。

以下是程序的代码:

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView textView = findViewById(R.id.text_view);

textView.setText("Hello World!");

}

}

2. Calculator

这是一个小型计算器小程序,它可以用来进行简单的数学运算。这个程序不仅可以帮助您了解 Android 的布局和事件处理,还可以让您熟悉如何使用各种 Android 组件。

以下是程序的代码:

public class MainActivity extends AppCompatActivity {

private EditText mOperandOneEditText;

private EditText mOperandTwoEditText;

private TextView mResultTextView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mOperandOneEditText = findViewById(R.id.operand_one_edit_text);

mOperandTwoEditText = findViewById(R.id.operand_two_edit_text);

mResultTextView = findViewById(R.id.result_text_view);

Button addButton = findViewById(R.id.add_button);

addButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int operandOne = Integer.parseInt(mOperandOneEditText.getText().toString());

int operandTwo = Integer.parseInt(mOperandTwoEditText.getText().toString());

int result = operandOne + operandTwo;

mResultTextView.setText(String.valueOf(result));

}

});

}

}

3. Weather

这是一个天气预报小程序,它可以显示当前位置的天气情况。这个程序可以帮助您了解 Android 的网络编程和权限管理。

以下是程序的代码:

public class MainActivity extends AppCompatActivity {

private static final String API_KEY = "YOUR_API_KEY";

private LocationManager mLocationManager;

private TextView mLocationTextView;

private TextView mTemperatureTextView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mLocationTextView = findViewById(R.id.location_text_view);

mTemperatureTextView = findViewById(R.id.temperature_text_view);

mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);

return;

}

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);

}

private LocationListener mLocationListener = new LocationListener() {

@Override

public void onLocationChanged(Location location) {

double latitude = location.getLatitude();

double longitude = location.getLongitude();

String url = "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid=" + API_KEY;

new WeatherTask().execute(url);

}

@Override

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

@Override

public void onProviderEnabled(String provider) {}

@Override

public void onProviderDisabled(String provider) {}

};

private class WeatherTask extends AsyncTask {

@Override

protected String doInBackground(String... urls) {

try {

URL url = new URL(urls[0]);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

StringBuilder sb = new StringBuilder();

String line;

while ((line = reader.readLine()) != null) {

sb.append(line).append("\n");

}

reader.close();

connection.disconnect();

return sb.toString();

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

@Override

protected void onPostExecute(String s) {

super.onPostExecute(s);

try {

JSONObject jsonObject = new JSONObject(s);

JSONObject mainObject = jsonObject.getJSONObject("main");

double temperature = mainObject.getDouble("temp") - 273.15;

String location = jsonObject.getString("name") + ", " + jsonObject.getJSONObject("sys").getString("country");

mTemperatureTextView.setText(String.format("%.1f", temperature) + "°C");

mLocationTextView.setText(location);

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

以上是几个必看的 Android 小程序,它们非常适合初学者和有经验的开发者。通过学习这些程序的代码,您可以更好地了解 Android 开发的基本概念和技术。