参考Android平台提供的API接口并实现一个具有定位、相机、网络权限请求功能的Android应用程序
时间: 2024-03-09 17:46:04 浏览: 60
Android Camera2 实现预览功能
以下是一个具有定位、相机、网络权限请求功能的Android应用程序的示例代码:
MainActivity.java
```java
package com.example.permissionsdemo;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity implements LocationListener {
private static final int REQUEST_LOCATION_PERMISSION = 1;
private static final int REQUEST_CAMERA_PERMISSION = 2;
private static final int REQUEST_INTERNET_PERMISSION = 3;
private LocationManager mLocationManager;
private TextView mLocationTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button locationButton = findViewById(R.id.location_button);
Button cameraButton = findViewById(R.id.camera_button);
Button internetButton = findViewById(R.id.internet_button);
mLocationTextView = findViewById(R.id.location_text_view);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
getLocation();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
}
}
});
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
}
}
});
internetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET)
== PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Internet permission already granted", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INTERNET},
REQUEST_INTERNET_PERMISSION);
}
}
});
}
private void getLocation() {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this);
}
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_LOCATION_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
} else {
Toast.makeText(this, "Location permission denied", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == REQUEST_INTERNET_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Internet permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Internet permission denied", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onLocationChanged(@NonNull Location location) {
mLocationTextView.setText("Latitude: " + location.getLatitude() + "\nLongitude: " + location.getLongitude());
}
@Override
public void onProviderDisabled(@NonNull String provider) {
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
```
activity_main.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/location_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Location" />
<TextView
android:id="@+id/location_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Location: " />
<Button
android:id="@+id/camera_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Camera" />
<Button
android:id="@+id/internet_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check Internet Permission" />
</LinearLayout>
```
在这个示例中,我们首先在Activity中声明了所需的三个权限:ACCESS_FINE_LOCATION、CAMERA和INTERNET。
然后,在我们的Activity的onCreate方法中,我们设置了三个button的点击监听器,以便在用户单击按钮时请求所需的权限。
我们还实现了LocationListener接口,以便在位置变化时更新UI中的位置信息。
最后,在onRequestPermissionsResult方法中,我们处理用户的权限请求响应,并根据用户的响应执行相应的操作。
请注意,这只是一个示例代码,您需要根据您的具体应用程序需要进行调整。同时,您还需要在Manifest文件中声明这些权限,以确保您的应用程序在运行时具有所需的权限。
阅读全文