Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
show Fragment on LockScreen (for Android)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/show_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show overlay" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="daichan4649.lockoverlay"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="daichan4649.lockoverlay.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="OverlayActivity"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" />
<service android:name="OverlayService" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_close" />
</LinearLayout>
package daichan4649.lockoverlay;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShowOverlay = (Button) findViewById(R.id.show_overlay);
btnShowOverlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
launchOverlayService();
finish();
}
});
}
private void launchOverlayService() {
Intent intent = new Intent(MainActivity.this, OverlayService.class);
startService(intent);
}
}
package daichan4649.lockoverlay;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class OverlayActivity extends Activity {
private static final String TAG = OverlayActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "[onCreate]");
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// fragment表示
Fragment fragment = getFragmentManager().findFragmentByTag(FragmentType.OVERLAY.getTag());
if (fragment == null) {
fragment = OverlayFragment.newInstance();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, fragment, FragmentType.OVERLAY.getTag());
ft.commit();
}
private enum FragmentType {
OVERLAY("overlay");
private String tag;
private FragmentType(String tag) {
this.tag = tag;
}
public String getTag() {
return tag;
}
}
}
package daichan4649.lockoverlay;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class OverlayFragment extends Fragment {
public static OverlayFragment newInstance() {
OverlayFragment fragment = new OverlayFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_overlay, container, false);
Button close = (Button) view.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getActivity().finish();
}
});
return view;
}
}
package daichan4649.lockoverlay;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
public class OverlayService extends Service {
private static final String TAG = OverlayService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerOverlayReceiver();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
unregisterOverlayReceiver();
super.onDestroy();
}
private void registerOverlayReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(ACTION_DEBUG);
registerReceiver(overlayReceiver, filter);
}
private void unregisterOverlayReceiver() {
unregisterReceiver(overlayReceiver);
}
// am broadcast -a daichan4649.lockoverlay.action.DEBUG
private static final String ACTION_DEBUG = "daichan4649.lockoverlay.action.DEBUG";
private BroadcastReceiver overlayReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "[onReceive]" + action);
if (action.equals(Intent.ACTION_SCREEN_ON)) {
// ACTON_SCREEN_ON はコードからのみ登録可
showOverlayActivity(context);
} else if (action.equals(ACTION_DEBUG)) {
showOverlayActivity(context);
}
}
};
private void showOverlayActivity(Context context) {
Intent intent = new Intent(context, OverlayActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.