책을 펼처보니 브로드캐스트(이하 BC) 전체 1개의 장에 밑줄 그어가면서 본 사람이 있네
각각의 BC에 퍼미션을 주는게 있고 안 주는게 있는것 같은데,
어느 BC가 퍼미션이 필요한지 모르는 상태라
그냥 일단 다 메니페스트에 퍼미션 주고 해야 하나?
그리고, 주석처리한 부분은 안되는 이유를 모르겠다.
삭제한 기능인가? 상위api에서 사용가능할까?
package com.example.broadcast;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
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.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView ivBattery;
EditText edtBattery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("브로드캐스트");
ivBattery = (ImageView) findViewById(R.id.imageView1);
edtBattery = (EditText) findViewById(R.id.editText1);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(br);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter iFilter = new IntentFilter();
// iFilter.addAction(action.equals(Intent.ACTION_BATTERY_CHANGED);
iFilter.addAction(Intent.ACTION_TIME_TICK);
iFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
iFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
iFilter.addAction(Intent.ACTION_BATTERY_LOW);
iFilter.addAction(Intent.ACTION_BATTERY_OKAY);
iFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
iFilter.addAction(Intent.ACTION_CAMERA_BUTTON);
iFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
iFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
iFilter.addAction(Intent.ACTION_DATE_CHANGED);
iFilter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
iFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
iFilter.addAction(Intent.ACTION_DOCK_EVENT);
iFilter.addAction(Intent.ACTION_GTALK_SERVICE_CONNECTED);
iFilter.addAction(Intent.ACTION_GTALK_SERVICE_DISCONNECTED);
iFilter.addAction(Intent.ACTION_HEADSET_PLUG);
iFilter.addAction(Intent.ACTION_INPUT_METHOD_CHANGED);
iFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
iFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
iFilter.addAction(Intent.ACTION_MEDIA_BUTTON);
iFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
iFilter.addAction(Intent.ACTION_MEDIA_NOFS);
iFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
iFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
iFilter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
iFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
iFilter.addAction(Intent.ACTION_MEDIA_SHARED);
iFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTABLE);
iFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
iFilter.addAction(Intent.ACTION_MY_PACKAGE_REPLACED);
iFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
iFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
iFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
iFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
iFilter.addAction(Intent.ACTION_PACKAGE_FIRST_LAUNCH);
iFilter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED);
iFilter.addAction(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION);
iFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
iFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
iFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
iFilter.addAction(Intent.ACTION_POWER_CONNECTED);
iFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
iFilter.addAction(Intent.ACTION_SCREEN_OFF);
iFilter.addAction(Intent.ACTION_SCREEN_ON);
iFilter.addAction(Intent.ACTION_SHUTDOWN);
iFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
iFilter.addAction(Intent.ACTION_TIME_CHANGED);
iFilter.addAction(Intent.ACTION_TIME_TICK);
iFilter.addAction(Intent.ACTION_UID_REMOVED);
// iFilter.addAction(Intent.ACTION_DREAMING_STARTED);
// iFilter.addAction(Intent.ACTION_DREAMING_STOPPED);
// iFilter.addAction(Intent.ACTION_PACKAGE_VERIFIED);
registerReceiver(br, iFilter);
}
BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String toastContent = "\n추가 노";
if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED))
toastContent = "비행기모드 바뀜 \n";
// else if (action.equals(action.equals(Intent.ACTION_BATTERY_LOW))
// toastContent = "Battery low.";
// else if (action.equals(action.equals(Intent.ACTION_BATTERY_OKAY))
// toastContent = "Battery okay.";
// else if (action.equals(action.equals(Intent.ACTION_BOOT_COMPLETED))
// toastContent = "Boot completed." ;
// else if (action.equals(action.equals(Intent.ACTION_CAMERA_BUTTON))
// toastContent = "Camera button pressed." ;
else if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
toastContent = "System dialog closed." ;
else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED))
toastContent = "Configuration changed." ;
else if (action.equals(Intent.ACTION_DATE_CHANGED))
toastContent = "Date changed." ;
else if (action.equals(Intent.ACTION_DEVICE_STORAGE_LOW))
toastContent = "Device storage low." ;
else if (action.equals(Intent.ACTION_DEVICE_STORAGE_OK))
toastContent = "Device storage OK." ;
else if (action.equals(Intent.ACTION_DOCK_EVENT))
toastContent = "Dock event." ;
else if (action.equals(Intent.ACTION_GTALK_SERVICE_CONNECTED))
toastContent = "GTalk service connected." ;
else if (action.equals(Intent.ACTION_GTALK_SERVICE_DISCONNECTED))
toastContent = "GTalk service disconnected." ;
else if (action.equals(Intent.ACTION_HEADSET_PLUG))
toastContent = "Headset plugged or unplugged." ;
else if (action.equals(Intent.ACTION_INPUT_METHOD_CHANGED))
toastContent = "Input method changed." ;
else if (action.equals(Intent.ACTION_LOCALE_CHANGED))
toastContent = "Locale changed." ;
else if (action.equals(Intent.ACTION_MEDIA_BAD_REMOVAL))
toastContent = "Media bad removal." ;
else if (action.equals(Intent.ACTION_MEDIA_BUTTON))
toastContent = "Media button pressed." ;
else if (action.equals(Intent.ACTION_MEDIA_CHECKING))
toastContent = "Checking media." ;
else if (action.equals(Intent.ACTION_MEDIA_EJECT))
toastContent = "Media eject requested." ;
else if (action.equals(Intent.ACTION_MEDIA_MOUNTED))
toastContent = "External media is mounted." ;
else if (action.equals(Intent.ACTION_MEDIA_NOFS))
toastContent = "External media is incompatible." ;
else if (action.equals(Intent.ACTION_MEDIA_REMOVED))
toastContent = "External media is removed." ;
else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED))
toastContent = "Scanning external media is finished." ;
else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE))
toastContent = "Requests the media scanner to scan a file and adds it to the media database.";
else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED))
toastContent = "The media scanner has started scanning a directory." ;
else if (action.equals(Intent.ACTION_MEDIA_SHARED))
toastContent = "External media is unmounted because it is being shared via USB mass storage.";
else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTABLE))
toastContent = "External media is present but cannot be mounted." ;
else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED))
toastContent = "External media is present, but not mounted at its mount point.";
else if (action.equals(Intent.ACTION_MY_PACKAGE_REPLACED))
toastContent = "A new version of your application has been installed over an existing one.";
else if (action.equals(Intent.ACTION_NEW_OUTGOING_CALL))
toastContent = "An outgoing call is about to be placed." ;
else if (action.equals(Intent.ACTION_PACKAGE_ADDED))
toastContent = "A new application package has been installed on the device.";
else if (action.equals(Intent.ACTION_PACKAGE_CHANGED))
toastContent = "An existing application package has been changed." ;
else if (action.equals(Intent.ACTION_PACKAGE_DATA_CLEARED))
toastContent = "The data of a package has been cleared." ;
else if (action.equals(Intent.ACTION_PACKAGE_FIRST_LAUNCH))
toastContent = "An application is first launched." ;
else if (action.equals(Intent.ACTION_PACKAGE_FULLY_REMOVED))
toastContent = "An existing application package has been completely removed from the device.";
else if (action.equals(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION))
toastContent = "A package needs to be verified." ;
else if (action.equals(Intent.ACTION_PACKAGE_REMOVED))
toastContent = "An existing application package has been removed from this device.";
else if (action.equals(Intent.ACTION_PACKAGE_REPLACED))
toastContent = "A new version of an application package replaced an existing version previously installed.";
else if (action.equals(Intent.ACTION_PACKAGE_RESTARTED))
toastContent = "A package has been restarted, and all of its processes have been killed.";
else if (action.equals(Intent.ACTION_POWER_CONNECTED))
toastContent = "External power has been connected to the device." ;
else if (action.equals(Intent.ACTION_POWER_DISCONNECTED))
toastContent = "External power has been removed from the device." ;
else if (action.equals(Intent.ACTION_SCREEN_OFF))
toastContent = "Screen turns off." ;
else if (action.equals(Intent.ACTION_SCREEN_ON))
toastContent = "Screen turns on." ;
else if (action.equals(Intent.ACTION_SHUTDOWN))
toastContent = "This device is shutting down." ;
else if (action.equals(Intent.ACTION_TIMEZONE_CHANGED))
toastContent = "The timezone has changed." ;
else if (action.equals(Intent.ACTION_TIME_CHANGED))
toastContent = "The time was set.";
else if (action.equals(Intent.ACTION_TIME_TICK))
toastContent = "The current time has changed.";
else if (action.equals(Intent.ACTION_UID_REMOVED))
toastContent = "A user ID has been removed from the system." ;
else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
int remain = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
edtBattery.append("\n 현재 충전량 : " + remain + " %\n");
if (remain >= 90)
ivBattery.setImageResource(R.drawable.battery_100);
else if (remain >= 70)
ivBattery.setImageResource(R.drawable.battery_80);
else if (remain >= 50)
ivBattery.setImageResource(R.drawable.battery_60);
else if (remain >= 10)
ivBattery.setImageResource(R.drawable.battery_20);
else ivBattery.setImageResource(R.drawable.battery_0);
int plug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
switch (plug) {
case 0:
edtBattery.append("전원 연결 : 안됨");
break;
case BatteryManager.BATTERY_PLUGGED_AC:
edtBattery.append("전원 연결 : 어댑터 연결됨");
break;
case BatteryManager.BATTERY_PLUGGED_USB:
edtBattery.append("전원 연결 : USB 연결됨");
break;
}
}
edtBattery.append(toastContent);
// else if (action.equals(Intent.ACTION_PACKAGE_VERIFIED))
// toastContent = "A package is verified." ;
// else if (action.equals(Intent.ACTION_DREAMING_STARTED))
// toastContent = "Dreaming started." ;
// else if (action.equals(Intent.ACTION_DREAMING_STOPPED))
// toastContent = "Dreaming stopped." ;
}//public void onReceive
};
}
댓글 0