I have a video watch app. I want to watch video in locked screen, and it is working. but some Api version i can watch but can't change video, can't open in rotated screen in some Api version. My code is belove
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
keyguardManager.requestDismissKeyguard(this, null);
}
WindowManager wm= (WindowManager) getSystemService(WINDOW_SERVICE);
Display dp= wm.getDefaultDisplay();
if (dp.getHeight()<dp.getWidth())
{
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
setContentView(R.layout.activity_watch_landscape);
}
else {
setContentView(R.layout.activity_watch);}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
When ı delete this code line, problem solved
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
keyguardManager.requestDismissKeyguard(this, null);
Related
In my application I want to use service and in this service I should show layout.
I want when click on button show dialog, I write below codes!
But after click on button, show this dialog back of layout and not show any dialog!
My service codes :
public class FloatingLayoutService extends Service implements StepperFormListener {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//Inflate the layout using LayoutInflater
layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
floatingLayout = (ConstraintLayout) layoutInflater.inflate(R.layout.service_floating_layout, null);
floatingLay_root = floatingLayout.findViewById(R.id.floatingLay_root);
floatingLay_main = floatingLayout.findViewById(R.id.floatingLay_main);
floatingLay_emptyImg = floatingLayout.findViewById(R.id.floatingLay_emptyImg);
...
//Set layout params to display the controls over any screen.
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
LAYOUT_FLAG,
0,
PixelFormat.TRANSLUCENT);
// From API26, TYPE_PHONE deprecated. Use TYPE_APPLICATION_OVERLAY for O
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
params.type = WindowManager.LayoutParams.TYPE_PHONE;
//Initial position of the floating controls
params.gravity = Gravity.BOTTOM | Gravity.START;
params.x = 0;
params.y = 0;
//Add the controls view to windowManager
windowManager.addView(floatingLayout, params);
//Task page btn
floatingLay_infoContentNextPage.setOnClickListener(v -> {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Are you sure?")
.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
});
return START_STICKY;
}
How can I show dialog, front of layout ?
You can make an activity without a layout just to show the Alert dialog.
public class MyAlertDialog extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
...
...
.show()
}
}
Remember to finish the activity.
You can set the title and message dynamically by passing them through intent.
Service
Intent intent = new Intent(this, MyAlertDialog.class);
intent.putExtra("titleKey", "title");
intent.putExtra("messageKey", "messge");
startActivity(intent);
And inside the activity, read it as:
getIntent().getStringExtra("titleKey")
getIntent().getStringExtra("messageKey")
After loading in all the current song information to the session metadata, the album art refuses to load on the lockscreen. I know for a fact that the bitmap is valid because I use the same resource for the notification art. I've also tried using just a static resource, but no luck. Any help would be much appreciated.
String more;
if (shuffled) {
more = Integer.toString(shufflePosition+1) + "/" + Integer.toString(playlist.size());
}
else{
more = Integer.toString(position+1) + "/" + Integer.toString(playlist.size());
}
int notificationAction = android.R.drawable.ic_media_pause;//needs to be initialized
//Build a new notification according to the current state of the MediaPlayer
if (mp.isPlaying()) {
notificationAction = android.R.drawable.ic_media_pause;
} else{
notificationAction = android.R.drawable.ic_media_play;
}
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),np.getArt());
} catch (IOException e) {
bitmap = BitmapFactory.decodeResource(getResources(),
R.mipmap.empty_track);
e.printStackTrace();
}
mSession.setMetadata(new MediaMetadataCompat.Builder()
.putBitmap(MediaMetadata.METADATA_KEY_ART, bitmap)
.putString(MediaMetadata.METADATA_KEY_ARTIST, np.getArtist())
.putString(MediaMetadata.METADATA_KEY_ALBUM, np.getAlbum())
.putString(MediaMetadata.METADATA_KEY_TITLE, np.getTrack())
.build());
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notification = new android.support.v7.app.NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.mipmap.empty_artist)
.setShowWhen(false)
.addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_media_previous, "Previous", pendingPrev).build())
.addAction(new NotificationCompat.Action.Builder(notificationAction, "Pause", pendingPlay).build())
.addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_media_next, "Next", pendingNext).build())
.setColor(getResources().getColor(R.color.colorPrimary))
.setContentTitle(np.getTrack())
.setContentText(np.getArtist())
.setSubText(more)
.setPriority(Notification.PRIORITY_MAX)
.setLargeIcon(bitmap)
.setContentIntent(launchActivity)
.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle()
.setMediaSession(mSession.getSessionToken()).setShowActionsInCompactView(0,1,2))
.build();
} else {
notification = new android.support.v7.app.NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.mipmap.empty_artist)
.setShowWhen(false)
.addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_media_previous, "Previous", pendingPrev).build())
.addAction(new NotificationCompat.Action.Builder(notificationAction, "Pause", pendingPlay).build())
.addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_media_next, "Next", pendingNext).build())
.setColor(getResources().getColor(R.color.colorPrimary))
.setContentTitle(np.getTrack())
.setContentText(np.getArtist())
.setSubText(more)
.setPriority(Notification.PRIORITY_MAX)
.setLargeIcon(bitmap)
.setContentIntent(launchActivity)
.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle()
.setMediaSession(mSession.getSessionToken())
.setShowActionsInCompactView(0,1,2))
.build();
}
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(001, notification);
startForeground(001, notification);
As per the Working with a Media Session documentation page:
In Android 4.0 (API level 14) and greater, the background of the lock screen displays your album artwork - but only if the media session metadata includes a background bitmap.
You must set a background Bitmap in your MediaMetadataCompat by using putBitmap with either METADATA_KEY_ALBUM_ART or METADATA_KEY_ART
You have to use playbackstate after 3 days i found this one
public void pausePlayer() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
startForeground(false);
((MainActivity) context).notificationUpdated(false);
isPlay = false;
playbackStateCompat.setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_STOP);
playbackStateCompat.setState(PlaybackStateCompat.STATE_PAUSED, mediaPlayer.getCurrentPosition(), 1.0f, SystemClock.elapsedRealtime());
mediaSession.setPlaybackState(playbackStateCompat.build());
}
}
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(currentSong.getUrl()));
mediaSession.setMetadata(getMetadata());
playbackStateCompat.setActions(PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_STOP);
playbackStateCompat.setState(PlaybackStateCompat.STATE_PLAYING, mediaPlayer.getCurrentPosition(), 1.0f, SystemClock.elapsedRealtime());
mediaSession.setPlaybackState(playbackStateCompat.build());
mediaPlayer.start();
My app is supposed to draw an overlay (blue light filter) at a certain time. Now I have a method called
private void drawOverlay()
Which under normal circumstances works perfectly fine. However, I want to use this method from a service, that runs in the background and is already running, so that I can draw the overlay at a certain time. In some cases this works, but sometimes I get a the following error.:-
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
I understand this has something to do with the UI thread, and I am supposed to use a Looper and a handler and I have read the documentation, but I cannot fully work out how I am supposed to do this.
Can someone please clarify this.
private void drawOverlay() {
log("drawing overlay");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
log("overlay >m checking permission");
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
if (permissionGranted) {
log("permission granted");
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.screen_dimmer, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
PixelFormat.TRANSLUCENT);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(view, params);
overlayDrawn = true;
} else {
log("no permission");
}
} else {
log("overlay drawing no permission needed (<m)");
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
if (permissionGranted) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.screen_dimmer, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
PixelFormat.TRANSLUCENT);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(view, params);
}
}
}
Before the service is started the app checks if it has permission to draw an overlay, that is where the permissionGranted boolean comes from, this is only needed if the SDK version is higher than M.
How can I crop camera intent in Android 4.4?
I can crop camera intent below 4.4 with
Intent pictureActionIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < 19) {
pictureActionIntent.putExtra("crop", "true");
pictureActionIntent.putExtra("outputX", 100);
pictureActionIntent.putExtra("outputY", 120);
pictureActionIntent.putExtra("aspectX", 1);
pictureActionIntent.putExtra("aspectY", 1);
pictureActionIntent.putExtra("scale", true);
pictureActionIntent.putExtra("return-data", true);
} else {
}
startActivityForResult(pictureActionIntent, 1);
but in 4.4 it's not working on else portion.
There are several open source libraries for cropping images in Android.
Intent action from an app that may not exist on any given user’s device.
Here are some libraries to consider:
https://github.com/biokys/cropimage
https://github.com/MMP-forTour/cropimage (forked from the above one)
protected void onPause()
{
super.onPause();
// If the screen is off then the device has been locked
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
//screen locked
if (!isScreenOn) {
boolean pressed = onKeyDown(26, null);
//power button pressed
if(pressed){
//remove keyguard
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
//start intent
Intent i = new Intent(this, VoiceRecognitionActivity.class);
startActivity(i);
}
}
}
the above code does is when power button is pressed, the keyguard will be dismissed and the activity onpaused will be resumed.
However, the keyguard is not dimissed when i pressed the power button, and i have to unlock manually.
When i pressed the power button, the window of my activity flashed for a second and the keyguard window is shown.
If you want to prevent phone from turning screen off (and locking the phone in result) you should use WakeLock. You can use PowerManager.newWakeLock() with FLAG_KEEP_SCREEN_ON or even
FULL_WAKE_LOCK.
This code snippet may help:
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
// Turn on the screen unless we are being launched from the AlarmAlert
// subclass.
final boolean screenOff = getIntent().getBooleanExtra(SCREEN_OFF, false);
if (!screenOff) {
try {
// API 8+
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
} catch (final Throwable whocares) {
// API 7+
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
}