activity_main.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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="308dp"
android:layout_height="215dp"
android:layout_marginStart="47dp"
android:layout_marginTop="52dp"
android:layout_marginEnd="56dp"
android:text="방송된 내용"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView myText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (TextView) findViewById(R.id.textView1);
}
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_LOW);
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(receiver, filter);
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) { // 방송이 왔을 때
String action = intent.getAction();
Toast.makeText(context, action, Toast.LENGTH_LONG).show();
myText.setText(action);
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
int maxvalue = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
int value = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int level = value * 100 / maxvalue;
myText.setText(action + "\n현재 배터리 레벨 : " + level);
}
else if (action.equals(Intent.ACTION_BATTERY_LOW)) {
myText.setText(action + "\n배터리가 부족한 상황입니다.");
}
}
};
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
'안드로이드 Android' 카테고리의 다른 글
[모바일프로그래밍] 11-3 외부 저장 공간(SD 카드) 사용 (0) | 2020.11.22 |
---|---|
[모바일프로그래밍] 11-2 내부 저장 장소를 사용하는 경우 (0) | 2020.11.22 |
[모바일프로그래밍] 10-4 연결 타입 서비스 bind service (0) | 2020.11.08 |
[모바일프로그래밍] 10-3 Intent service class (0) | 2020.11.08 |
[모바일프로그래밍] 10-2 서비스 (0) | 2020.11.08 |