I have two receivers
<receiver
android:name=".BootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".DateReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
This are their java implementations
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("BootReceiver", "ok");
}
}
public class DateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("DateReceiver", "new date");
}
}
I'd like to make a single receiver with all intent-filters and make a switch when it receives an intent.
<receiver
android:name=".DataAndBootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
public class DataAndBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()
.equals("android.intent.action.DATE_CHANGED")) {
Log.i("Receiver", "Data");
} else {
Log.i("Receiver", "Boot");
}
}
}
Add it in manifest like this
<receiver android:name=".EventHandler" >
<intent-filter>
<action android:name="android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT" />
<action android:name="android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED" />
<action android:name="android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED" />
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.intent.action.HEADSET_PLUG"/>
<action android:name="android.hardware.action.NEW_PICTURE" />
<action android:name="android.hardware.action.NEW_VIDEO" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
and receive them something like that....
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "---------------------------------------");
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_AIRPLANE_MODE_CHANGED)){
// Do Some Thing here...
}
}
Related
my program extends From BroadCastReciever to set Alarm, after the device is reboot receiver is not called, this is my code
1- BroadcastReceiver
public class myReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()!=null) {
if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
Log.d("myActivity","repooted");
// alarm settings
}
}
}
2-xml
<receiver
android:name=".sync.myReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
In your AndroidManifest.xml have you added permission to receive boot complete?
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
I am using a Broadcast reciever to auto start my app whenever phone is switched on. But it is not working in my case. I am attaching the program details. Please somebody help me, where am I lagging behind. Thanks in advance
this is manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vishal.readsimnumber">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".reciever.BootStartReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".FetchSimNumber"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
this is my broadcast reciever
public class BootStartReciever extends BroadcastReceiver {
public BootStartReciever() {
}
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, FetchSimNumber.class);
context.startService(pushIntent);
}
}
}
this is my service class
public class FetchSimNumber extends Service {
public FetchSimNumber() {
Intent intent = new Intent(FetchSimNumber.this,MainActivity.class);
startActivity(intent);
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
and this is my main activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
String getSimNumber = telemamanger.getLine1Number();
((TextView) findViewById(R.id.tv1)).setText(getSimSerialNumber);
((TextView) findViewById(R.id.tv2)).setText(getSimNumber);
}
}
public class BootStartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
/* Setting the service here */
Intent i = new Intent(context, Yourservice.class)
context.startService(i);
}
and in your manifest file:
<receiver android:name=".BootReceiver"
android:enable="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
sometimes category.HOME not work so you can use category.DEFAULT also.. i dont know the reason behind this, but its working fine..
AndroidManifest.xml
<receiver android:name=".Receiver">
<intent-filter android:priority="1000000">
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Receiver.xml
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
**do your stuff at here...**
}
}
I have declared receivers in Manifest.xml like this:
<receiver android:name=".receivers.MyTrackerReceiver"
android:exported="true">
<intent-filter android:priority="998">
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true"
android:permission="android.permission.INSTALL_PACKAGES">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
and my custom receiver is
public class MyTrackerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String referrerString = extras.getString("referrer");
Log.v("referrer ", referrerString);
SharedPreferenceUtils.getInstance(context).setReferrer(referrerString);
new CampaignTrackingReceiver().onReceive(context, intent);
}
}
but the onReceive in CampaignTrackingReceiverdoesn't get called, what I am missing,thanks!
It's working now there was some problem with sending broadcast to receiver
I want to start my application whenever the phone is unlocked.
Below is my code which works fine when the phone is restarted, but I also want it to work whenever the phone is unlocked.
What should I do?
I want to start my application every time the user unlocks the phone. What did I do wrong in my code?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.installedapps22"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:icon="#drawable/cherry_icon" android:label="#string/app_name">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BootUpReciever" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".ListInstalledApps" > </activity>
<activity android:name=".TabsLayoutActivity" />
</application>
</manifest>
public class BootUpReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("in broad....");
if ((intent.getAction() != null) &&
(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
{
System.out.println("in broadcast receiver.....");
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
check manifist file
<activity
android:name="com.wallop.tech.MyAndroidAppActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:name="Startup" >
<intent-filter android:enabled="true" android:exported="false">
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.SCREEN_OFF"/>
</intent-filter>
</receiver>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Startup extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null) {
if( intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent s = new Intent(context, MyAndroidAppActivity.class);
s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(s);
}
}
}
}
I am using broadcast receivers for performing some action when the device is switched off and switched on again,but they are not working.These are the receivers in manifest file:
<receiver android:name=".ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
<receiver android:name=".RestartReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
These are the corresponding classes:
public class ShutdownReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("In","Switched Off");
}
}
public class RestartReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
SecureMessagesActivity.ToDoOnMobileSwitchOn();
Log.d("In","Switched On");
}
}
Please help me.Thanks in advance.
Be sure to request the RECEIVE_BOOT_COMPLETED permission in your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>