Reputation: 354
Here is my Receiver file. Which I feel doesn't have any kind of problem. I just prints the type of intent which is passed. According to me onReceive is never called. I guess there is a problem with my manifest itself? Or any other ideas? The Receiver is not linked to any anyother code in the application.
package sharukh.locky;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadCastReceiver extends BroadcastReceiver
{
public BroadCastReceiver()
{
}
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("rece", intent.getAction());
}
}
My Manifest also looks good, but I can't figure out why it isn't working.
<?xml version="1.0" encoding="utf-8"?>
<manifest package="sharukh.locky"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyAGufoN9huWkmw8nTskk2aaFHW1gXn1Z7Q"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<!-- Activities -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Tutorial"/>
<activity android:name=".TutorialSelectApp"/>
<activity
android:name=".LockScreen"
android:label="@string/title_activity_lock_screen"
android:theme="@style/AppTheme.NoActionBar"/>
<!-- Services -->
<service
android:name=".LockyService"
android:enabled="true"
android:exported="true"/>
<receiver
android:name=".BroadCastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.HEADSET_PLUG"/>
</intent-filter>
</receiver>
</application>
</manifest>
I haven't dynamically added/registered/unregistered any receivers. I would require some help here.
Upvotes: 1
Views: 1605
Reputation: 2613
You can do this by trying dynamic calling of broadcast like below
public class YourActivity extends Activity {
//Create broadcast object
BroadcastReceiver mBroadcast = new BroadcastReceiver() {
//When Event is published, onReceive method is called
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("[BroadcastReceiver]", "MyReceiver");
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("[BroadcastReceiver]", "Screen ON");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.d("[BroadcastReceiver]", "Screen OFF");
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
}
Upvotes: 2