Tuesday, 26 July 2016

Object-Oriented Programming with Android Applications

Object-oriented programming  a big change in mobile application development and programming. An object-oriented program is, at its heart, designed to be modified. Using correctly written software, you can take advantage of features that are already built in, add new features of your own, and override features that don’t suit your needs.

For example, You have and employee with name and job title . Employee has many other attributes but now we are considering some basic:
class Employee {
  String name;
  String jobTitle;
}
Any other company has different types of employees. For example, In your company may have full-time and part-time employees. Every full-time employee has a yearly salary:
class FullTimeEmployee extends Employee {
  double salary;
}
The words extends Employee means the new class "FullTimeEmployee" has all the properties  of the "Employee class"
Like any Employee, a FullTimeEmployee has a name and a jobTitle. But a FullTimeEmployee also has a salary. 
For example part-time employee has no fixed yearly salary. Instead, every part-time employee has an hourly pay rate and a certain number of hours worked in a week:
class PartTimeEmployee extends Employee {
  double hourlyPay;
  int hoursWorked;
}
So by this a PartTimeEmployee has four characteristics: namejobTitle,hourlyPay, and number of hoursWorked.
Now for example you have to create a class for  executives. Every executive is a full-time employee. But in addition every executive will receive bonus  (even if the company goes belly-up and needs to be bailed out):
class Executive extends FullTimeEmployee {
  double bonus;
}
Java's extends keyword is very useful because, by extending a class, you inherit all the complex code that's already in the inherited class. The class you extend can be a class that you have (or another developer has) already written. One way or another, you're able to reuse existing code and to add ingredients to the existing code.
Now For example : The  Android wrote the Activity class, with its 4,000 lines of code. You get to use all those lines of code for free by simply typing extends Activity:
public class MainActivity extends Activity {
With these two words extends Activity, your new MainActivity class can do all the things that another Android activity can do : 

Monday, 25 July 2016

How to detect Android default pattern lock failed and success events

How to Detect Android pattern lock  Events.


Step 1: Create a android project with android studio.
Step 2 : Create a class "AdminReceiver" and put the following code into the class.







public class AdminReceiver extends DeviceAdminReceiver {
@Override public void onEnabled(Context ctxt, Intent intent) {
ComponentName cn=new ComponentName(ctxt, AdminReceiver.class);
DevicePolicyManager mgr=
(DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
mgr.setPasswordQuality(cn,
DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);
onPasswordChanged(ctxt, intent);
}
@Override public void onPasswordChanged(Context ctxt, Intent intent) {
DevicePolicyManager mgr=
(DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
int msgId;
if (mgr.isActivePasswordSufficient()) {
msgId=R.string.compliant;
}
else {
msgId=R.string.not_compliant;
}
Toast.makeText(ctxt, msgId, Toast.LENGTH_LONG).show();
}
@Override public void onPasswordFailed(Context ctxt, Intent intent) {
Toast.makeText(ctxt, R.string.password_failed, Toast.LENGTH_LONG)
.show();
}
@Override public void onPasswordSucceeded(Context ctxt, Intent intent) {
Toast.makeText(ctxt, R.string.password_success, Toast.LENGTH_LONG)
.show();
}
}

Step3: Into the main activity class put the following code into your oncreate method.

ComponentName cn=new ComponentName(this, AdminReceiver.class);
DevicePolicyManager mgr=
(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
if (mgr.isAdminActive(cn)) {
int msgId;
if (mgr.isActivePasswordSufficient()) {
msgId=R.string.compliant;
}
else {
msgId=R.string.not_compliant;
}
Toast.makeText(this, msgId, Toast.LENGTH_LONG).show();
}
else {
Intent intent=
new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getString(R.string.device_admin_explanation));
startActivity(intent);
}
finish();
}
This code will get the admin permission from the user and when user gives the permission the service will run.

Step4 : Your manifest should be look like this add the broadcast intent to receive the pattern.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.commonsware.android.pwenforce" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18"/>
<uses-feature android:name="android.software.device_admin" android:required="true"/>
<application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name="MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="AdminReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
<action android:name="android.app.action.ACTION_PASSWORD_CHANGED"/>
<action android:name="android.app.action.ACTION_PASSWORD_FAILED"/>
<action android:name="android.app.action.ACTION_PASSWORD_SUCCEEDED"/>
</intent-filter>
</receiver>
</application>
</manifest>



 You can download the sample code from here