This post will explain with an example how to receive the Screen On event.
Firstly create an application with the following details:
activity_screen.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScreenActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
ScreenActivity.java
package com.samples.screenonreceive;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.Toast;
public class ScreenActivity extends Activity {
// Broadcast Receiver to Screen On
private BroadcastScreenReceiver mBroadcastScreenReceiver;
// Specify that the Application should receive Screen On
private IntentFilter mIntentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
// Register
mBroadcastScreenReceiver = new BroadcastScreenReceiver();
mIntentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
registerReceiver(mBroadcastScreenReceiver, mIntentFilter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.screen, menu);
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
// Unregister
if (mBroadcastScreenReceiver != null) {
unregisterReceiver(mBroadcastScreenReceiver);
}
}
/**
* Broadcast Receiver to Screen ON
* @author Tanvi Surve
*
*/
private class BroadcastScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Make this Toast appear on Screen On
Toast.makeText(getApplicationContext(), "Screen is ON", Toast.LENGTH_LONG).show();
}
}
}
Run the application:
Use the highlighted button on the emulator to make the screen ON and OFF. On the Screen ON, you will get a Message as "Screen is ON".
Excellent Blog, I appreciate your hard work , it is useful
ReplyDeletesimply superb, mind-blowing, I will share your blog to my friends also
Android Online Training
I am really enjoying reading your well written articles.
ReplyDeleteIt looks like you spend a lot of effort and time on your blog.
I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
Android Training Institute in Chennai | Android Training Institute in anna nagar | Android Training Institute in omr | Android Training Institute in porur | Android Training Institute in tambaram | Android Training Institute in velachery