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>
Related
I have a several devices (Minix X68-i) all running the same firmware and software versions.
On some of them my boot receiver works fine, others it rarely works, and a few it's closer to 50-50 whether they will receive the event.
I'm out of ideas as to what could cause the code to work so erratically, other than potentially faulty hardware.
Manifest:
<receiver android:enabled="true" android:exported="true"
android:name="com.proreception.displaymanagement.Receiver.BootReceiver"
android:label="StartMyActivity" >
<intent-filter android:priority="500" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BootReceiver
public class BootReceiver extends BroadcastReceiver {
private static final String TAG = "BootReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) && constants.ONBOOT) {
Intent i = new Intent(context, FullscreenActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, "Starting Application");
context.startActivity(i);
}
}
}
Is it possible to develop one application that if incoming call is coming I need to open my android app, in that I have to answer/reject the call and another option that is divert call. If i click on divert button call needs to transfer to another person. I dont have idea on this concept. Can we do it like this?
Manifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<receiver android:name="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
<receiver android:name="MyPhoneReceiver" >
<intent-filter>
<action android:name="tuet" >
</action>
</intent-filter>
</receiver>
<activity android:name="StartActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MyPhoneReceiver.JAVA
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.e("DEBUG", phoneNumber);
}
}
}
}
StartActivity.JAVA
public class StartActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent("tuet");
sendBroadcast(intent);
}
}
Thank you!
I have a customer that needs a custom launcher to make his devices like a kiosk, just because he needs the device have limited access to limited applications. Everything is ok so far.
One of the most important feature he wants is the possibility to increase/decrease the applications that can be launch in his devices. I wrote the GCM receiver and it's also ok.
My problem is: I want to send a custom broadcast from the GCM service to my main activity (the home activity). I think the code is OK, but the broadcast is never received in main activity. Any ideas?
My manifest:
<activity
android:name="jv.android.atmlauncher.activity.HomeScreen"
android:excludeFromRecents="true"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="nosensor" >
<intent-filter>
<action android:name="jv.antroid.atmlauncher.apklistchanged" /> <!-- This is my custom broadcast -->
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
My Service:
#Override
public void onHandleIntent(Intent intent) {
...
Intent intent = new Intent("jv.antroid.atmlauncher.apklistchanged");
sendBroadcast(intent);
}
My home activity:
private BroadcastReceiver listapkReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
registerReceiver(listapkReceiver, new IntentFilter("jv.antroid.atmlauncher.apklistchanged"));
listapkReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
... Do something.
}
};
}
#Override
protected void onDestroy() {
unregisterReceiver(listpkReceiver);
super.onDestroy();
}
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" />
In my android application I am starting the activity using broadcast receiver. If my device lock during this activity and if I unlock it then activity restart mean it run on create again
Please help me to solve this problem
Thanks in advance
What's your activity declaration in AndroidManifest.xml?
I think u should appoint launchMode to "singleTask" like that:
<activity android:name=".Youracticity" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait">
</activity>
^-^
In your Manifest do you have anything like this:
<action android:name="android.intent.action.USER_PRESENT" />
<receiver android:name="com.activities.app" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
If yes, then please handle it properly.
for detect screen on and screen off register a broadcast reciver like:
<receiver android:name="receiverScreen">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.Intent.ACTION_USER_PRESENT" />
</intent-filter>
</receiver>
In Activity or Service:
try {
IntentFilter if= new IntentFilter(Intent.ACTION_SCREEN_ON);
if.addAction(Intent.ACTION_SCREEN_OFF);
if.addAction(Intent.ACTION_USER_PRESENT);
BroadcastReceiver mReceiver = new receiverScreen();
registerReceiver(mReceiver, if);
} catch (Exception e) {
}
receiver code where System inform you if Screen on/off happen:
public class receiverScreen extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
}
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
}
}
}