0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Kotlinで地図アプリ(ゴミ拾いアプリ)。(12日目)

0
Posted at

今日(昨日)やった事

・MySQLの自動バックアップ
・Kotlinで制作したスマホアプリに、ユーザー側でカスタム用の設定画面の作成

■MySQLのバックアップ

MySQLのバックアップを自宅サーバのubuntuで自動で取る簡単なシェルスクリプトを作成。

ロリポップサーバ側

MySQL_backup.sh
mysqldump --skip-column-statistics  --single-transaction -u(ID) -p(PASS) -h (データベースホスト) (データベース名) >  /hoge/huga/hunyaaaa/backup/MySQL-Trash_$(date +%Y%m%d).sql

自宅サーバ側(ubuntu)

loli_backup.sh
# !/bin/sh
$ scp -i ~/.ssh/lolipop_rsa  -P 2222 pinoko.jp-(ID)@ssh.lolipop.jp:/home/hoge/huga/hunyaaaa/backup/MySQL-SSH_$(date +%Y%m%d).sql /home/hoge/huga/hunyaaaa/backup/
chmod 600 /home/hoge/huga/hunyaaaa/backup/*

これを、cronに登録

その他、MySQLの勉強用メモ

my.cnfの場所と順番を確認する方法

bash
$ mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

参考サイト:

Kotlinのスマホアプリに設定画面を作成

リソースの[layout]に設定画面用に、新しいレイアウトをファイル(activity_setting.xml)を作成

設定画面用のレイアウト作成

[app]
 ->[res]
 ->[layout]
 ->[activity_setting.xml]

activity_setting.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fee"
    tools:context=".MapsActivity">
 
    <TextView
        android:text="設定画面"
        android:textSize="30sp"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.3"/>
 
    <Button
        android:id="@+id/return_button"
        android:text="戻る"
        android:textSize="30sp"
        android:layout_margin="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
</androidx.constraintlayout.widget.ConstraintLayout>

「tools:context=".MapsActivity"」を、環境に合わせて「.MainActivity」などに変更

実験なので「android:text」には、そのまま文字を入力

SettingActivity.ktを作成

[app]
 ->[java]
 ->[パッケージ名]
 ->[SettingActivity.kt]

SettingActivity.kt
package パッケージ名]
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class SettingActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_setting)
        setContentView(R.layout.activity_setting)
        val btnBack :Button = findViewById(R.id.return_button)
        btnBack.setOnClickListener {
            finish()
        }
    }
}

MapsActivity.kt
....
        when (view.id) {
            //設定画面
            R.id.main_set->{
                val intent = Intent(this, SettingActivity::class.java)
                startActivity(intent)
            }

ERROR発生

ここまで解説サイトを参考に作成したあと、実行してみると、強制終了でダウン。

Logcatを見ると、以下のようなERROR文を発見。

・・・.SettingActivity}; have you declared this activity in your AndroidManifest.xml?

AndroidManifestに追記する必要あり?

AndroidManifest.xml を変更

とりあえず、ざっと追加。

イメージ7483.jpg

無事動作。
イメージ7484.jpg
戻るボタンをクリック(finish())で元の画面に戻りました。
イメージ7485.jpg

Activity間のデータ送受信

画面推移が出来るようになったので、Activity間のデータ送受信もやってみます。

参考サイト:

intent.putExtra(...)を追加

MapsActivity.kt
....
        val intent = Intent(this, SettingActivity::class.java)
        intent.putExtra("TEXT_KEY","テキスト送信")//追加
        startActivity(intent)
SettingActivity.kt
....
        val text = intent.getStringExtra("TEXT_KEY")
        Toast.makeText(this, text, Toast.LENGTH_LONG).show()

イメージ7489.jpg

途中までですが、今日(昨日)はここまで。

参考サイト:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?