I am struggling to make my basic lockscreen app.starting, the app displays a basic activity screen, with an image view (displaying an image from storage) and a TextView (displaying date & time).On swiping up, the activity goes back. What I am struggling with is, when screen is turned off & turned on, the event receiver should send out this event & my activity should come to foreground again. That's not what's happening. I am not completely sure how to use my service & broadcastreceiver to achieve this. Any help would be appreciated please.
My AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kjtest2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<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:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<receiver
android:name="com.example.kjtest2.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="com.example.kjtest2.myService"/>
<activity
android:name="com.example.kjtest2.MainActivity"
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>
My receiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class EventsReciever extends BroadcastReceiver {
public 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, MainActivity.class);
context.startActivity(intent);
}
else if(recievedIntent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
}
}
}
My onCreate code in the activity:
protected void onCreate(Bundle savedInstanceState) {
Log.i("event", "activity onCreate");
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, EventsReciever.class);
setContentView(R.layout.activity_main);
/** REMOVING KEYGUARD RECEIVER **/
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
setTime();
changeImage();
ImageView img2 = (ImageView)findViewById(R.id.imageView);
img2.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeLeft() {
Toast.makeText(MainActivity.this, "Swipe left", Toast.LENGTH_SHORT)
.show();
changeImage();
Log.i("event","onSwipeLeft");
}
@Override
public void onSwipeRight() {
TextView tvDisplayDate = (TextView)findViewById(R.id.date1);
CustomDigitalClock cdc = (CustomDigitalClock)findViewById(R.id.dc1);
if (tvDisplayDate.getVisibility()==View.VISIBLE) {
tvDisplayDate.setVisibility(View.GONE);
cdc.setVisibility(View.GONE);
} else {
tvDisplayDate.setVisibility(View.VISIBLE);
cdc.setVisibility(View.VISIBLE);
}
}
@Override
public void onSwipeUp() {
Toast.makeText(MainActivity.this, "Swipe up", Toast.LENGTH_SHORT)
.show();
MainActivity.this.moveTaskToBack(true);
}
@Override
public void onSwipeDown() {
Toast.makeText(MainActivity.this, "Swipe down", Toast.LENGTH_SHORT)
.show();
}
});
}
And finally my service:
public class myService extends Service{
private NotificationManager mNM;
private int NOTIFICATION = R.string.local_service_started;
private final IBinder mBinder = new MyBinder();
private Messenger outMessenger;
@Override
public void onCreate() {
/** INITIALIZE RECEIVER **/
//RegisterReciever();
Log.i("event", "ServiceStarted");
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Display a notification about us starting. We put an icon in the status bar.
showNotification();
}
}
As of now, the receiver & service aren't starting. I had been able to start them by registering the event dynamically, but I don't think that should be required. Registering the event in manifest should be sufficient for my application, isn't it?
Thanks for helping.
make sure your service is running by including startService(new Intent(this,MyService.class));
statement in your activity onCreate()
it is actually required to register receiver programmatically, manifest declaration will not work for Intent.ACTION_SCREEN_OFF
and Intent.ACTION_SCREEN_ON
, register the receiver instance in service onCreate()
, unregister in onDestroy()
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
receiver = new EventsReciever();
registerReceiver(receiver, filter);