Here's the deal, I've been working on making a simple screen locker app that mainly does the following:
removes the keygaurd
disables home and back buttons
runs whenever the screen goes ON after going OFF
runs at boot [not tested yet]
I've DONE MY HOMEWORK and used tons of links (especially on stackoverflow) and still so many problems
number 1 works perfectly :)
number 2 works but not as intended, when I press it, it open the dialog that asks the user for choosing a home application... I don't want that! I want to make a lock screen app, not a home app. (code is provided at the end)
number 3 works before unlocking the screen, but after that, the application doesn't know how to start itself automatically. I've implemented a Broadcast Receiver that is registered in code in the onCreat() method for the lock screen activity. I think this is the problem :S
number 4 this doesn't work at all!
I was testing with a service, but still not working :'(
Code
Manifest I'm sure I'm using the the right permissions, am I?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="himura.test.mylockertest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<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"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<receiver
android:name=".EventsReciever"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service
android:enabled="true"
android:name=".UpdateService"/>
<activity
android:name=".LockPage"
android:label="#string/app_name" >
<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>
</application>
</manifest>
activity the layout is very simple, just one button to unlock :)
public class LockPage extends Activity {
private Button ublockButton;
#Override
public void onAttachedToWindow() {
//this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
///** FIRST THINGS FIRST, START THE SERVICE **/
//startService(new Intent(this, myService.class));
/** REGISTERING RECEIVER **/
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
BroadcastReceiver mReceiver = new EventsReciever();
registerReceiver(mReceiver, filter);
/** SETTING CONTENT VIEW**/
setContentView(R.layout.lockscreen);
/** REMOVING KEYGUARD RECEIVER **/
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
/** NORMAL CODE **/
ublockButton = (Button)findViewById(R.id.bUnlock);
ublockButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
#Override
public void onBackPressed() {
// Don't allow back to dismiss.
return;
}
//only used in lockdown mode
#Override
protected void onPause() {
super.onPause();
Log.i("event","onPause");
// Don't hang around.
finish();
}
#Override
protected void onStop() {
super.onStop();
Log.i("event","onStop");
// Don't hang around.
finish();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
}
Broadcast Receiver this handles 3 things, screen on/off and boot complete (or it should),,, I've been testing with Logs
public class EventsReciever extends BroadcastReceiver {
//works before unlocking
//after unlocking, keygaurd still off, but receiver has stopped
public static boolean wasScreenOn = true;
#Override
public void onReceive(Context context, Intent recievedIntent) {
Log.i("Check","[BroadCastReciever] onRecieve()");
if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
Log.i("Check","[BroadCastReciever] Screen went OFF");
} else if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wasScreenOn = true;
Log.i("Check","[BroadCastReciever] Screen went ON");
Intent intent = new Intent(context, LockPage.class);
context.startActivity(intent);
}
else if(recievedIntent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent intent = new Intent(context, LockPage.class);
context.startActivity(intent);
// 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");
}
}
}
Finally, the service doesn't do anything now, I was trying to use it to start the lock screen after it gets unlocked for the first time
public class myService extends Service{
#Override
public void onCreate() {
/** INITIALIZE RECEIVER **/
//RegisterReciever();
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The standard pattern for implementing a Service is to create and run a new thread from onStartCommand
// to perform the processing in the background and stop the Service when it’s complete
//RegisterReciever();
return Service.START_STICKY;
}
/*private void RegisterReciever(){
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
BroadcastReceiver mReceiver = new EventsReciever();
registerReceiver(mReceiver, filter);
}*/
}
more issues I've found on the internet includes the return of the status bar after turning the screen on (very bad)
in the native keyguard, there is the status bar, but it doesn't function, is there a way to do that?
isn't there a way to just make the locker activity, and tell the system that here u go, use this as the keyguard?
isn't there a way to just make the locker activity, and tell the system that here u go, use this as the keyguard?
If you create your own firmware, presumably there is a way to replace the keyguard with an alternative implementation, considering that most device manufacturers do it. You cannot replace the keyguard via the SDK.
to solve number 2 problem(remove Lock from Home application) remove below line from AndroidManifest.xml file
< category android:name="android.intent.category.HOME" />
Service:
BroadcastReceiver mReceiver=null;
int ReadingBlock = 100;
String PasswordType;
#Override
public void onCreate() {
super.onCreate();
// Register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new AutoStart();
registerReceiver(mReceiver, filter);
}
#Override
public void onStart(Intent intent, int startId) {
boolean screenOn = false;
try{
// Get ON/OFF values sent from receiver ( AEScreenOnOffReceiver.java )
screenOn = intent.getBooleanExtra("screen_state", false);
}catch(Exception e){}
if (!screenOn){
} else {
Intent RegularPassword = new Intent(getApplicationContext(), ScreenLock.class);
RegularPassword.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(RegularPassword);
}
}
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onDestroy() {
Log.i("ScreenOnOff", "Service distroy");
if(mReceiver!=null)
unregisterReceiver(mReceiver);
}
Related
I have an application that I would like to have automatically start following boot completion. The following code seems overly complicated and I get erratic application starts when swiping to a neighbouring workspace.
What am I missing here? I have an activity class, a service class, as well as a broadcast receiver. Below is my code (in that order) followed by the manifest.
public class BlueDoor extends Activity implements OnClickListener{
Button btnExit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);
btnExit = (Button) this.findViewById(R.id.ExitButton);
btnExit.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ExitButton:
System.exit(0);
break;
}
}
}
service.class
public class BlueDoorStartService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setClass(this, BlueDoor.class);
startActivity(callIntent);
// do something when the service is created
}
}
broadcast receiver
public class StartBlueDoorAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, BlueDoorStartService.class);
context.startService(serviceIntent);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluedoor"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".StartBlueDoorAtBootReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".BlueDoorStartService" >
</service>
<activity
android:name=".BlueDoor"
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>
UPDATE Solution(s), 10/22/2015:
Changing the service to:
public class BlueDoorStartService extends Service {
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
}
and the receiver to:
public class StartBlueDoorAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Start Service On Boot Start Up
Intent serviceIntent = new Intent(context, BlueDoorStartService.class);
context.startService(serviceIntent);
//Start App On Boot Start Up
Intent App = new Intent(context, BlueDoor.class);
App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(App);
}
}
resulted in a working configuration using a service w/no misbehaving. However deleting the service all together and modifying the receiver thus:
public class StartBlueDoorAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent App = new Intent(context, BlueDoor.class);
App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(App);
}
}
also resulted in a functional as well as a more concise configuration that starts the application following boot completion.
Your BroadcastReceiver calls
context.startService(serviceIntent)
so the service will be created if it doesn't exist yet (which will be the case shortly after booting) and thus start the activity from its onCreate() method. So the app works, to a certain extent.
BUT when you call startService(), the system always calls the service's onStartCommand() method. You did not override that method, so the system uses the standard implementation from class android.app.Service.
As you can read on grepcode.com, the method will return a value like START_STICKY by default. This tells the system to keep the service alive until it is explicitly stopped.
In your case, I suppose the system reacted to the swiping by temporarily killing and then reanimating (= creating) the service, which in turn started your activity.
Some information on the service lifecycle can be found here.
What you can do:
Override onStartCommand() to start the activity from there instead of from onCreate(). Then use stopSelf(int) like described here
One last thing: when exiting from the activity, don't use System.exit(0) but call finish() instead, see this SO answer for "why".
I am trying to create an application that could respond when the power button is pressed. To be more specific, which would respond to it when pressed 2 or 3 times.
For now, I tried the following:
public class SMSKey extends BroadcastReceiver{
static int countPowerOff = 0;
private Activity activity = null;
public SMSKey(Activity activity){
this.activity = activity;
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
countPowerOff++;
}else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
if(countPowerOff == 2){
Intent i = new Intent(activity, SMSOptions.class);
activity.startActivity(i);
}
}
}
}
and in my manifest:
<receiver android:name=".SMSKey">
<intent-filter >
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
</intent-filter>
</receiver>
finally in my MainActivty.java:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
SMSKey mReceiver = new SMSKey(this);
registerReceiver(mReceiver, filter);
Even though this works, it only works for the 1st time, it won't work on the 2nd or 3rd attempt when the power button is pressed. Why is that so ??
And another question: as you can see, I am using this KeyPress event in my MainActivity, which means the application is to be open all the time. Is there any other way that I can implement this without getting into the MainActivity.
This is not even an Android problem. You never reset your countPowerOff variable after you have received the 3 key presses. Even after having done that you must consider adding an alarm that will reset your countPowerOff variable to zero after some small timeout. It will allow you to avoid situations where the user does not intend to interact with your application and just presses the button, but it still gets counted.
As to your second question, try implementing an IntentService.
Here is the solution
public class MyReceiver extends BroadcastReceiver {
private static int countPowerOff = 0;
public MyReceiver (){
}
#Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
Log.e("In on receive", "In Method: ACTION_SCREEN_OFF");
countPowerOff++;
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
Log.e("In on receive", "In Method: ACTION_SCREEN_ON");
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
Log.e("In on receive", "In Method: ACTION_USER_PRESENT");
if (countPowerOff >= 2)
{
countPowerOff=0;
Toast.makeText(context, "MAIN ACTIVITY IS BEING CALLED ", Toast.LENGTH_LONG).show();
Intent i = new Intent(context, About.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
}
I don't have much experience in Android dev. Right now, i am unable to register a receiver from the onStartCommand method of a service.
In english it would be : I have two buttons (Start/Stop service) on an Activity. When I click on the Start button, I wan't to run the service which will register the BroadcastReceiver (SMS_RECEIVED). When a SMS is received, I wan't to see a log trace. But I don't see it !
It seems that I can't register my Broadcast receiver but I know that my Service is running (thanks to the logs).
In addition, I'd like to make my service, and consequently my BroadcastReceiver, persistent (if I quit the application, I want it to run in background, and even if I restart the phone).
Can anyone tell me what's wrong in my code ? ... and maybe give me help with my second question .... :)
Thanks !
Here is my code :
--- Activity : Main ---
package com.tuto.servicerunbroadcastreceiver;
import XYZ
public class Main extends Activity implements OnClickListener
{
Button bt_start;
Button bt_stop;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_start = (Button)findViewById(R.id.button1);
bt_start.setOnClickListener(this);
bt_stop = (Button)findViewById(R.id.button2);
bt_stop.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1 :
{
Log.d("Button : ", "Button start");
startService(new Intent(this, svcMessage.class));
break ;
}
case R.id.button2 :
{
Log.d("Button : ", "Button stop");
stopService(new Intent(this, svcMessage.class));
break ;
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
--- Service : svcMessage ---
package com.tuto.servicerunbroadcastreceiver;
import XYZ;
public class svcMessage extends Service
{
private static final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED";
private BroadcastReceiver br_receiver;
#Override
public IBinder onBind(Intent arg0)
{
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
final IntentFilter filter = new IntentFilter();
filter.addAction("ACTION_RECEIVE_SMS");
Log.d("Service : ", "start");
this.br_receiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("LOG : ", "onReceive");
}
};
this.registerReceiver(this.br_receiver, filter);
return (START_STICKY);
}
#Override
public void onDestroy()
{
super.onDestroy();
Log.d("Service : ", "destroy");
this.unregisterReceiver(this.br_receiver);
}
}
--- Manifest.xml ---
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tuto.servicerunbroadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.tuto.servicerunbroadcastreceiver.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.tuto.servicerunbroadcastreceiver.svcMessage">
</service>
</application>
</manifest>
It seems you have a simple bug here:
//without ""
filter.addAction(ACTION_RECEIVE_SMS);
and don't forget add the permission:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
One more thing, if you unregister the receiver in the onDestory(). you should register in onCreate() on Service.
I want to implement a minimal screen activity logger app. So, the application should run on the background (no user interaction) and it will log the screen on and off activities. I have started these codes, but it seems that I need to register my ScreenBroadcastReceiver broadcastreceiver. If I do it with the below code in main activity, it works. However, I do not want to register it in main because the user cannot launch the activity every time. So, where should i register my BroadcastReceiver so that the application works without user interaction?
oncreate in main activity
//I need to find another place to put these code, Where ???
//IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
//filter.addAction(Intent.ACTION_SCREEN_OFF);
//BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
//registerReceiver(screenOnReceiver, filter);
This is ScreenBroadcastReceiver, it will be triggered when the screen is on.
public class ScreenBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.w("OnReceive", "SCREEN IS ON");
}
}
}
This is the BootReceiver to run the program on the background itself.
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, ScreenListenerService.class);
context.startService(service);
//IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
//filter.addAction(Intent.ACTION_SCREEN_OFF);
//BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
//context.registerReceiver(screenOnReceiver, filter);
}
}
This is the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".ScreenListenerService"></service>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Update:
I added a service, but it does not work. Did i forget to add something? or what ?
public class ScreenListenerService extends Service {
public void OnCreate(){
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
registerReceiver(screenOnReceiver, filter);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
use Service for your purpose and do the same as you did in the activity.
I want to open my application immediately when S Pen is detached , How can you do this, if the methods put under onSPenDetached is only called when my application is opened again?
Thanks,
Chandu
The following works on my Galaxy Tab A 9.7 with S-Pen (SM-P550) running Android 5.0.2.
Attaching and detaching the stylus creates Broadcast Intents of type com.samsung.pen.INSERT with a booleanExtra named penInsert of false if detached and true if put back into the device.
Thus a Broadcast Receiver can be created that filters this kind of events. The following code is for such a Broadcast Receiver which starts OneNote if the stylus is detached:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class SPenDetachIntentBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent penInsertIntent) {
if (!penInsertIntent.getBooleanExtra("penInsert", true)) {
try {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.microsoft.office.onenote");
context.startActivity(launchIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
In the Manifest file you need to declare it as a receiver listening for com.samsung.pen.INSERT Broadcast Intents with an intent filter. The following entry in a project's AndroidManifest.xml declares SPenDetachBroadcastReceiver, generates an instance and makes it listening for com.samsung.pen.Insert Broadcast Intents:
<receiver
android:name=".SPenDetachIntentBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.samsung.pen.INSERT" />
</intent-filter>
</receiver>
The advantage over using registerSPenDetachmentListener on an SPenEventLibrary object to register a Service with an onSPenDetached method implemented is that you do not need any additional library files and you also do not need additional permissions.
You will need to create a BroadcastReceiver and a Service.
The service:
public class SPenService extends Service {
SPenEventLibrary mSPenEventLibrary = new SPenEventLibrary();
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
mSPenEventLibrary.registerSPenDetachmentListener(this, new SPenDetachmentListener() {
#Override
public void onSPenDetached(boolean bDetached) {
if (bDetached) {
Toast.makeText(SPenService.this, "S Pen Detached", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SPenService.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(SPenService.this, "S Pen Inserted", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
mSPenEventLibrary.unregisterSPenDetachmentListener(this);
}
}
The receiver:
public class SPenReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
context.startService(new Intent(context, SPenService.class));
}
}
}
The manifest (inside the <application> tag):
<receiver android:name=".SPenReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".SPenService" >
</service>