1.매니페스트
<service
android:name=".AwindowService"
android:enabled="true"
android:permission="android.permission.SYSTEM_ALERT_WINDOW">
</service>
2.권한
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 마시멜로우 이상일 경우
if (!Settings.canDrawOverlays(this)) { // 다른앱 위에 그리기 체크
Uri uri = Uri.fromParts("package" , getPackageName(), null);
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, uri);
startActivityForResult(intent, 2222);
} else {
}
} else {
}
3.서비스
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(this, AwindowService.class));
} else {
startService(new Intent(this, AwindowService.class));
}
public class AwindowService extends Service {
@Override
public IBinder onBind(Intent intent) { return null; }
@Override
public void onCreate() {
super.onCreate();
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, IntroActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = null;
contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder notificationBuilder = null;
try {
if (Build.VERSION.SDK_INT >= 26) {
String id = “idid;
CharSequence name = "idid";
String description = "idid desc";
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setLightColor(Color.RED);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
mChannel.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), audioAttributes);
mChannel.setShowBadge(true);
mNotificationManager.createNotificationChannel(mChannel);
notificationBuilder = new Notification.Builder(this, mChannel.getId())
.setSmallIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.icon : R.drawable.icon)
.setContentTitle(getString(R.string.app_name))
.setContentText("앱이 실행 중입니다.")
.setOngoing(true).setAutoCancel(false).setContentIntent(contentIntent);
} else {
notificationBuilder = new Notification.Builder(this)
.setSmallIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.icon : R.drawable.icon)
.setContentTitle(getString(R.string.app_name))
.setContentText("앱이 실행 중입니다.")
.setOngoing(true).setAutoCancel(false).setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE).setPriority(Notification.PRIORITY_MAX);
}
} catch (Exception e) {
e.printStackTrace();
}
Notification notification = notificationBuilder.build();
startForeground(1, notification);
}
@Override
public void onDestroy() {
super.onDestroy();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true); // Foreground service 종료
}
}
}
'안드로이드' 카테고리의 다른 글
android 타임아웃 주고 현재위치 찾기 (0) | 2024.02.28 |
---|---|
android webview popup (0) | 2024.01.30 |
WorkerManager android java (0) | 2024.01.17 |
안드로이드 문자 , 카카오톡 알림 캐치 (0) | 2024.01.16 |
Android Java 갤러리 다중이미지 가져와서 연속크랍(Crop) (0) | 2024.01.12 |