Let me use an example for what I'm looking for. On my phone I have a music player widget, when active and the phone times out, the player continues working (iPod style I get that). BUT when you turn the phone back on, the player is visible and active above the unlocking slide bar.
Is this easy to do in java? giving the user access to the application above the开发者_如何学Go unlock slider (before password).
Now I know we can easily put code to avoid sleep mode. But the app that I'm working on only needs to be viewed/modified every 10 or 20 minutes and keeping the phone on is a waste of battery.
This is a contentious issue:
There are apps that do this. See How to customize Android's LockScreen? e.g. WidgetLocker - so it is not entirely impossible.
However, Google does not recommend playing around with Android Lock Screens. Google has removed functionality for interacting with their OS lock screens (particularly as 2.2). More info on that here.
But some useful effects can be managed. There are tricks involved.
One trick is to replace the Android lock screen with a custom lock screen - essentially a Home Screen app that implements its own locking. I think that's how WidgetLocker works, but I'm not sure. There is a Home Screen demo app shipping with the Android SDK.
Another trick is to use the
ACTION_SCREEN_ON
andACTION_SCREEN_OFF
Intent actions, coupled withWindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
permission on your app. You can use these to change what is shown BEHIND or instead of the lock screen - which is very close to what the question asks (they need to view some info when the lock screen is in place).
I have an app that displays information to the user when the lock screen is shown, using the above techniques. Strictly speaking, it is not a custom Android lock screen, however, it creates the effect of customising the information shown when the phone is locked.
It shows info behind the actual stock lock screen. But you can also replace the screen that normally shows when the phone is locked.
Below is a (minimal) demo app that shows how to display information when the phone is locked. I forget where I got most of the code from, but its a mess of some demo ideas. I present it just to show what can be done - I am by no means saying this is anything like "good" production code.
When you run the app, and lock the phone, the app carries on playing the video. This screen replaces the lock screen.
It will take some work to turn this code into production code, but it may help readers.
YouTubeActivity.java:
The important part is the last 2 lines that enable this activity to be visible when the phone is locked.
package com.youtube.demo;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.VideoView;
public class YouTubeActivity extends Activity
{
String SrcPath = "rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView myVideoView = (VideoView) findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.requestFocus();
myVideoView.start();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
}
main.xml:
Nothing special here - just places a VideoView
on the screen...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="@+id/myvideoview"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml:
Absolutely stock manifest file - nothing interesting here.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.youtube.demo" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".YouTubeActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So there are 3 ways I can think of to accomplish what the poster is looking for, or workarounds that might be useful to him:
- Do what WidgetLocker does - I don't know how exactly, and the developer is not telling.
- Change the background behind the stock lock screen.
- Overlay an activity instead of the stock lock screen (demo code provided).
There is no API for drawing on lock screen, nor can you replace lock screen with a custom one. SO you can not write an app for stock android phones that does this.
In order to do this you'd need to create a custom firmware or use non-standard features in rooted phones.
精彩评论