Pythontr

husonet | Tarih: 18.04.2024

Android Ekran Kilidi Uygulaması

Android Ekran Kilidi LOCK SCREEN Uygulaması

LockScreenActivity.java
package com.pythontr.ekrankiliti;


import com.pythontr.ekrankiliti.LockService;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
// import android.widget.Toast;

public class LockScreenActivity extends Activity {
KeyguardManager km;
KeyguardManager.KeyguardLock kl;
boolean inDragMode;
int selectedImageViewX;
int selectedImageViewY;
int windowwidth;
int windowheight;
ImageView lock,phone,home;
//int phone_x,phone_y;
MarginLayoutParams marginParams2;
int home_x,home_y;
int[] droidpos;
private LayoutParams layoutParams;

@Override
public void onWindowFocusChanged(boolean hasFocus) {

home =(ImageView)findViewById(R.id.home);
lock =(ImageView)findViewById(R.id.droid);

if(getIntent()!=null&&getIntent().hasExtra("kill")&&getIntent().getExtras().getInt("kill")==1){
// Toast.makeText(this, "" + "kill activityy", Toast.LENGTH_SHORT).show();
finish();
}

try {
startService(new Intent(this,LockService.class));
StateListener phoneStateListener = new StateListener();
TelephonyManager telephonyManager =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);

windowwidth=getWindowManager().getDefaultDisplay().getWidth();
// System.out.println("windowwidth"+windowwidth);
windowheight=getWindowManager().getDefaultDisplay().getHeight();
// System.out.println("windowheight"+windowheight);


MarginLayoutParams marginParams1 = new MarginLayoutParams(home.getLayoutParams());
marginParams1.setMargins((windowwidth/24)*10,(windowheight/10)*8,0,0);
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(marginParams1);

home.setLayoutParams(layout);

marginParams2 = new MarginLayoutParams(lock.getLayoutParams());

marginParams2.setMargins((windowwidth/24)*10 + (home.getWidth() / 2) - (lock.getWidth()/2), (windowheight/10)*8 + 6, 0, 0);
RelativeLayout.LayoutParams layoutdroid = new RelativeLayout.LayoutParams(marginParams2);

// Toast.makeText(getApplicationContext(), "Deneme", Toast.LENGTH_SHORT).show();

lock.setLayoutParams(layoutdroid);

lock.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams = (LayoutParams) v.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
int[] hompos=new int[2];
// int[] phonepos=new int[2];
droidpos=new int[2];
//phone.getLocationOnScreen(phonepos);
home.getLocationOnScreen(hompos);
home_x=hompos[0];
home_y=hompos[1];
// phone_x=phonepos[0];
// phone_y=phonepos[1];
break;

case MotionEvent.ACTION_MOVE:

int x_cord = (int)event.getRawX();
int y_cord = (int)event.getRawY();

if(x_cord>windowwidth-(windowwidth/24)){x_cord=windowwidth-(windowwidth/24)*2;}
if(y_cord>windowheight-(windowheight/32)){y_cord=windowheight-(windowheight/32)*2;}

layoutParams.leftMargin = x_cord ;
layoutParams.topMargin = y_cord;

lock.getLocationOnScreen(droidpos);
v.setLayoutParams(layoutParams);

if(x_cord<home_x - 100)
{
v.setVisibility(View.GONE);
finish();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("/"));
startActivity(intent);
}
else if(x_cord>home_x + 100)
{
// v.setVisibility(View.GONE);
finish();
}
break;
case MotionEvent.ACTION_UP:
int x_cord1 = (int)event.getRawX();
// int y_cord2 = (int)event.getRawY();
if(x_cord1<home_x - 100)
{
//v.setVisibility(View.GONE);
//Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
//startActivity(intent);
//finish();
}
else
{
layoutParams.leftMargin = (windowwidth/24)*10 + (home.getWidth() / 2) - (lock.getWidth()/2);
// layoutParams.topMargin = (windowheight/32)*8;
layoutParams.topMargin = (windowheight/10)*8 + 6;
v.setLayoutParams(layoutParams);
}
break;
}
return true;
}
});

}catch (Exception e) {
// TODO: handle exception
}

}

@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG|WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onAttachedToWindow();
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.lock_screen);
}

class StateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {

super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("call Activity off hook");
finish();
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};

public void onSlideTouch( View view, MotionEvent event )
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:

int x_cord = (int)event.getRawX();
int y_cord = (int)event.getRawY();

if(x_cord>windowwidth){x_cord=windowwidth;}
if(y_cord>windowheight){y_cord=windowheight;}

layoutParams.leftMargin = x_cord -25;
layoutParams.topMargin = y_cord - 75;

view.setLayoutParams(layoutParams);

break;
default:
break;
}

}

@Override
public void onBackPressed() {
// Don't allow back to dismiss.
return;
}

//only used in lockdown mode
/*
@Override
protected void onPause() {
// System.exit(0);
super.onPause();

// Don't hang around.
// finish();
}
*/

// @Override
protected void onStop() {
super.onStart();
// super.onStop();

// Don't hang around.
// finish();
}


@Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)||(keyCode == KeyEvent.KEYCODE_POWER)||(keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)) {
//this is where I can do my stuff
// Toast.makeText(this, "Denem", Toast.LENGTH_LONG).show();
Log.e("test1", "Tıklandı ama açılmadı");
return true; //because I handled the event
}
if((keyCode == KeyEvent.KEYCODE_HOME)){
// Toast.makeText(this, "Denem1", Toast.LENGTH_LONG).show();
Log.e("HomeButonu", "Tıklandı ama açılmadı");
return true;
}
return false;

}


@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER ||(event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)||(event.getKeyCode() == KeyEvent.KEYCODE_POWER)) {
//Intent i = new Intent(this, NewActivity.class);
//startActivity(i);
// Toast.makeText(this, "Denem2", Toast.LENGTH_LONG).show();
Log.e("test1", "Tıklandı ama açılmadı");
return false;
}
if((event.getKeyCode() == KeyEvent.KEYCODE_HOME)){
// Toast.makeText(this, "Denem3", Toast.LENGTH_LONG).show();
Log.e("HomeButonu", "Tıklandı ama açılmadı");
// System.out.println("alokkkkkkkkkkkkkkkkk");
return true;
}
// return false;
return super.dispatchKeyEvent(event);
}

@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
// Log.i("LOG", "onUserLeaveHint");
//this.startActivity(this.getIntent());
// startActivity(new Intent(Intent.ACTION_VIEW, LockScreenActivity.class));
// System.exit(0);
//startActivity(getIntent());
//finish();
// startActivity(getIntent());
// Intent intent = new Intent(Intent.ACTION_VIEW,
// Uri.parse("/"));
// startActivity(intent);
}

@Override
protected void onResume() {
super.onResume();
}

/*public void unloack(){
finish();
}*/

@Override
public void onDestroy(){
// k1.reenableKeyguard();
super.onDestroy();

}

}


{reklam}

LockService.java
package com.pythontr.ekrankiliti;

import receiver.lockScreenReeiver;
import android.app.KeyguardManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

public class LockService extends Service{
BroadcastReceiver mReceiver;
// Intent myIntent;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}


@Override
public void onCreate() {
KeyguardManager.KeyguardLock k1;

//getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

KeyguardManager km =(KeyguardManager)getSystemService(KEYGUARD_SERVICE);
k1= km.newKeyguardLock("IN");
k1.disableKeyguard();




/*try{
StateListener phoneStateListener = new StateListener();
TelephonyManager telephonyManager =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}catch(Exception e){
System.out.println(e);
}*/

/* myIntent = new Intent(MyService.this,LockScreenAppActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle myKillerBundle = new Bundle();
myKillerBundle.putInt("kill",1);
myIntent.putExtras(myKillerBundle);*/

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);

mReceiver = new lockScreenReeiver();
registerReceiver(mReceiver, filter);


super.onCreate();


}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub

super.onStart(intent, startId);
}

/*class StateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {

super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("call Activity off hook");
getApplication().startActivity(myIntent);



break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};*/


@Override
public void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
}

StartLockScreen.java
package com.pythontr.ekrankiliti;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class StartLockScreen extends Activity {
/* KeyguardManager.KeyguardLock k1;

public void onAttachedToWindow() {
// TODO Auto-generated method stub
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this,LockService.class));
finish();


/*getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
KeyguardManager km =(KeyguardManager)getSystemService(KEYGUARD_SERVICE);
k1 = km.newKeyguardLock("IN");
k1.disableKeyguard();*/


}

}


lockScreenReceiver.java

package receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.pythontr.ekrankiliti.LockScreenActivity;

public class lockScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;

@Override
public void onReceive(Context context, Intent intent) {

//Toast.makeText(context, "" + "enterrrrrr", Toast.LENGTH_SHORT).show();
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//Toast.makeText(context, "" + "screeen off", Toast.LENGTH_SHORT).show();

wasScreenOn=false;
Intent intent11 = new Intent(context,LockScreenActivity.class);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent11);

// do whatever you need to do here
//wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

wasScreenOn=true;
Intent intent11 = new Intent(context,LockScreenActivity.class);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//context.startActivity(intent11);
//Toast.makeText(context, "" + "start activity", Toast.LENGTH_SHORT).show();
// and do whatever you need to do here
// wasScreenOn = true;
}
else if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
/* KeyguardManager.KeyguardLock k1;
KeyguardManager km =(KeyguardManager)context.getSystemService(context.KEYGUARD_SERVICE);
k1 = km.newKeyguardLock("IN");
k1.disableKeyguard();
*/
Intent intent11 = new Intent(context, LockScreenActivity.class);

intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent11);

// Intent intent = new Intent(context, LockPage.class);
// context.startActivity(intent);
// Intent serviceLauncher = new Intent(context, UpdateService.class);
// context.startService(serviceLauncher);
// Log.v("TEST", "Service loaded at start");
}

}


}

lock_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/mycolor1"
android:orientation="vertical">


<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/droid"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/unlock" />
</RelativeLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/homelinearlayout" >

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/locknob"
android:visibility="visible"
android:scaleType="fitXY"
android:id="@+id/home" />
</LinearLayout>

</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pythontr.ekrankiliti"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait"
android:name=".LockScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>
<activity
android:name=".StartLockScreen"
android:launchMode="singleInstance"
android:theme="@style/Theme.Transparent" >
<!--
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />


</intent-filter>
-->
</activity>

<service android:name=".LockService" >
</service>

<receiver
android:enabled="true"
android:name="receiver.lockScreenReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />

</intent-filter>
</receiver>

</application>

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

</manifest>


Kaynak: http://playingwithandroid.blogspot.com.tr/2012/07/making-customize-lock-screen.html