start an intent using actions on broadcast receiver - android

what i am trying to do is to start my app when the headset (plug in) in my android mobile here is my code , but nothing happens :
public class BootBroadcast extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
context.startService(new Intent(context, TestService.class));
Intent newIntent = new Intent(".android.intent.action.MAIN");
context.startActivity(newIntent);
}
}
and here is the manifest :
<activity
android:name="com.example.talktome.SplashScreen"
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=".BootBroadcast">
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
please any suggestions???

Have you added this snippet in your current activity?
IntentFilter receiverFilter = new IntentFilter(
Intent.ACTION_HEADSET_PLUG);
BootBroadcast receiver = new BootBroadcast();
registerReceiver(receiver, receiverFilter);
Also add this flag inside BootBroadcast class
newIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Add this in the manifest for second activity
<activity
android:name="com.example.talktome.TestService"
android:label="#string/app_name" />

Related

android.net.wifi.STATE_CHANGE intent handling (in Activity)

I would like to received a notification of the change of state of WiFi connection in my Android application.
I found several suggestion (here on SO) that mostly leads to the definition of an intent.
I'm using this approach:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controller);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
checkWiFi();
}
public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null)
checkWiFi();
}
};
That works but I'm wondering if I can use a Manifest centric approach i.e. if I put this in my AndroidManifest.xml:
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</activity>
I should be notified of the incoming intent . Right?
How can I handle this in my Activity?
Best regards, Mike
You should create class
MyBroadcastReceiver extends BroadcastReceiver
and next Manifest
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>

reciever is not working when phone is restarted

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...**
}
}

start activity B from service, and main activity Aalways be called first

I have a launcher activity called MainActivity.
MainActivity will start KeyGuardService, and KeyGuardService regist a ScreenOFF receiver, and then in onReceive will start LockScreenActivity use startActivity.
But everytime, MainActivity will be showed before LockScreenActivity. I have tried many solutions according to other Qustions-issue.
Menifest is as fllow:
<activity
android:name=".activities.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:taskAffinity="com.example.mylockscreen.main"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".services.KeyGuardService"
android:label="#string/app_name"/>
<activity
android:name=".activities.LockScreenActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:taskAffinity="com.example.mylockscreen.lock"
android:theme="#android:style/Theme.Translucent"/>
onReceive as follows
BroadcastReceiver mMasterResetReciever = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
Log.d(TAG, Constants.TAG + "service receive " + action);
if ((action.equals("android.intent.action.SCREEN_OFF"))
&& (Preferences.getUserPrefBoolean(context, "app_on", true)))
{
Intent localIntent = new Intent(context, LockScreenActivity.class);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
localIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
KeyGuardService.this.startActivity(localIntent);
Log.d(TAG, Constants.TAG + "service start activity");
}
}
};
can anyone tell me why, thanks.

How to launch the application upon booting up the device?

Can anybody share the sample code to how to launch android application upon starting/booting up the device?
This code will launch an application on start up. You need to listen for ACTION_BOOT_COMPLETE.
in AndroidManifest.xml (application-part):
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
[..]
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
[..]
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

Android : Why BroadcastReceiver crashing?

I have this Broadcast receiver registered
public class NotifyAlarmBroadcast extends BroadcastReceiver{
public Context context;
public static final String NOTI = "android.intent.action.MAIN";
// actually i want NOTI = "com.sumit.timekeeper.NotifyAlarm"
// this too is not working
// help me here please
#Override
public void onReceive(Context _context, Intent intent) {
context = _context;
Uri data = intent.getData();
String reason = intent.getStringExtra("alarm_reason");
Intent intentalarm = new Intent(NOTI, data);
intentalarm.putExtra("reason", reason);
context.startActivity(intentalarm);
}
}
and the manifest
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".TimeKeeperStartActivity"
android:screenOrientation="portrait" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotifyAlarm"
android:screenOrientation="portrait" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.sumit.timekeeper.NotifyAlarm">
</action>
</intent-filter>
</activity>
<receiver android:name=".NotifyAlarmBroadcast">
<intent-filter>
<action android:name="com.sumit.timekeeper.NotifyAlarmBroadcast" />
</intent-filter>
</receiver>
</application>
but when the line reaches context.startActivity(intentalarm);
the application crashes
may be it is where we pass first parameter to Intent, i'm not clear about
please help me.
Try to add the FLAG_ACTIVITY_NEW_TASK flag in your intent.
intentalarm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Categories

Resources