メイン画面の編集 ~常時GPSで緯度経度取得
左ペインの「strings.xml」をダブルクリックします
[app]⇒[res]⇒[values]⇒[strings.xml]をダブルクリックして開きます。以下ソースに書き換えます。
<resources>
<string name="app_name">LogGPS</string>
<string name="btnStart">位置情報取得サービスの開始</string>
<string name="btnStop">位置情報取得サービスの停止</string>
</resources>
左ペインの「activity_main.xml」をダブルクリックします
[app]⇒[res]⇒[layout]⇒[activity_main.xml]をダブルクリックして開きます。[Code]をクリックして、デザインからコードに切り替えます。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:paddingLeft="16dp"
android:paddingTop="4dp"
android:paddingRight="16dp"
android:text="@string/btnStart"
android:textAllCaps="false" />
<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:text="@string/btnStop"
android:textAllCaps="false" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp" />
</LinearLayout>
左ペインの「MainActivity.java」をダブルクリックします
[app]⇒[java]⇒[net.mam_mam.loggps]⇒[MainActivity.java]をダブルクリックします。以下ソースに丸ごと書き換えます。
package net.mam_mam.loggps;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;
import android.content.pm.PackageManager;
import android.content.Context;
import android.os.Build;
import android.content.Intent;
import android.util.Log;
import android.Manifest;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.content.DialogInterface;
import android.net.Uri;
import android.content.res.Configuration;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;
private Button btnStart;
private Button btnStop;
public Context ctx;
private String androidId;
@Override
protected void onCreate(Bundle savedInstanceState) {
ctx=this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PackageManager pm = getApplicationContext().getPackageManager();
Integer pmLocation =ContextCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION
);
if(pmLocation != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_PERMISSIONS_REQUEST_CODE);
}
Integer pmBackGround =ContextCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_BACKGROUND_LOCATION
);
if(pmBackGround != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Android11 以降は以下設定が必要です");
builder.setMessage(
"位置情報権限を「常に許可」に設定していただく必要があります。" +
"この画面の右下の[OK]ボタンを押しますと「アプリ情報」画面に遷移しますので、"+
"[権限]->[位置情報] を順にタップし、「常に許可」に設定してください。" +
"その後、本アプリケーションを再起動してください。"
);
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// ダイアログが閉じられた際の処理
Log.i("MainActivity", "setOnDismissListenerが呼ばれました。");
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
// This will take the user to a page where they have to click twice to drill down to grant the permission
startActivity(intent);
}
});
builder.show();
}else{
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
}
@Override
protected void onStart() { //Activityが開始され、まだユーザーには見えない時
super.onStart();
String androidId=Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
TextView tx=(TextView) findViewById(R.id.textView);
tx.setText("端末ID:"+androidId);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Serviceの開始
Intent intent = new Intent(getApplication(), LocationService.class);
//startService(intent);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ContextCompat.startForegroundService(ctx,intent);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplication(), LocationService.class);
stopService(intent);
}
});
}
}
「初めてのAndroid Studio体験記」に戻る
