Android: BroadcastReceiver onReceive not getting called - android

I am not able to figure out what am I writing wrong. This seems to a very simple case but I am not able to make this work.
1. registerReceiver is returning null, does that matters?
2. receiver's onReceive not getting called - is this linked to registerReceiver
giving null.
Below is code inside my activity
WifiManager mainWifiObj;
WifiScanReceiver wifiReceiver;
String wifis[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiScanReceiver();
final Button button = (Button) findViewById(R.id.nextButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mainWifiObj.startScan();
}
});
}
protected void onStop() {
unregisterReceiver(wifiReceiver);
super.onStop();
}
protected void onStart() {
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
//registerReceiver returning null
super.onStart();
}
class WifiScanReceiver extends BroadcastReceiver {
#SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
wifis = new String[wifiScanList.size()];
Log.v(this.getClass().getName(), "wifiScanList.size() : "
+ wifiScanList.size());
for (int i = 0; i < wifiScanList.size(); i++) {
wifis[i] = ((wifiScanList.get(i)).toString());
Log.v(this.getClass().getName(), wifiScanList.get(i).SSID);
}
}
}
and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.i2e1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FirstStateActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I took reference from http://www.tutorialspoint.com/android/android_wi_fi.htm
and tried reproducing similar code but it dint worked.
I am running this in Android emulator for Windows.

Related

BroadcastReceiver not working as expected on Android

I know this was asked already many times but I can't get this to work. I looked all around Android docs and other sources. I got this activity that has a broadcast receiver variable inside and starts a service as such in the constructor:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Received", Toast.LENGTH_SHORT).show();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compass);
Intent mIntent = new Intent(this, GPSTracker.class);
startService(mIntent);
}
#Override
public void onResume() {
super.onResume();
registerReceiver(mReceiver, new IntentFilter());
}
#Override
protected void onStop() {
unregisterReceiver(mReceiver);
super.onStop();
}
I put the service down in the manifest and I am sure it works properly. Any help will be appreciated.
Broadcast receiver is supposed to receive 2 floats from the service periodically.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jemboy.compass" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".GPSTracker"></service>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity android:name=".CompassActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherActivity"></activity>
</application>
In the activity
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(UPDATE_PLAYER)) {
updateMediaPlayerToggle();
} else if (intent.getAction().equals(BUFFERING)) {
showMediaPlayerBuffering();
}
}
};
private void registerBroadcastReceiver() {
LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter updateIntentFilter = new IntentFilter();
updateIntentFilter.addAction(UPDATE_PLAYER);
updateIntentFilter.addAction(BUFFERING);
broadcastManager.registerReceiver(broadcastReceiver, updateIntentFilter);
}
in the service.
private void sendUpdatePlayerIntent() {
Log.d(TAG, "updatePlayerIntent");
Intent updatePlayerIntent = new Intent(MainActivity.UPDATE_PLAYER);
LocalBroadcastManager.getInstance(this).sendBroadcast(updatePlayerIntent);
}
Example from a media player service.

Android Studio Estimote Beacon Application

im trying to make basic beacon application in android studio. I just want to scan beacons and list them into the screen. Here are my codes. I took them from somewhere.
public class MainActivity extends ActionBarActivity {
private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", null, null, null);
private BeaconManager beaconManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
#Override public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
Log.d("TAG", "Ranged beacons: " + beacons);
}
});
}
#Override
protected void onStart() {
super.onStart();
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
#Override public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e("TAG","Cannot start ranging", e);
}
}
});
}
#Override
protected void onStop() {
super.onStop();
try {
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e("TAG", "Cannot stop but it does not matter now", e);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
beaconManager.disconnect();
}
}
Here is my manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.oem.estimote_ibeacon_app" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
<service
android:name="com.estimote.sdk.service.BeaconService"
android:exported="false"/>
</application>
</manifest>
When i opened application it says "stopped" please help. I have beacons for test. Where is my mistake? Thank you.
it seems that ALL_ESTIMOTE_BEACONS in
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
is null.
enter a valid regionid instead of "regionid" in
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", null, null, null);

how to bind HCE hostapduservice to main activity on android

hy..i have a task to make my kitkat-nexus to act as a tag. I have ACS 122U as reader. i have read the program example in this site http://blog.opendatalab.de/hack/2013/11/07/android-host-card-emulation-with-acr122/. then i tryed the code on my own eclipse.
main activity :
public class MainActivity extends Activity implements OnMessageReceived, ReaderCallback {
private NfcAdapter nfcAdapter;
private ListView listView;
private IsoDepAdapter isoDepAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
isoDepAdapter = new IsoDepAdapter(getLayoutInflater());
listView.setAdapter(isoDepAdapter);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
Log.i("end of onCreate-----","onCreate HCE");
}
#Override
public void onResume() {
super.onResume();
//nfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
// null);
//nfcAdapter.disableReaderMode(this); //tambahan poipo
Log.i("onResume---", "onResume");
}
#Override
public void onPause() {
super.onPause();
nfcAdapter.disableReaderMode(this);
Log.i("onPause---", "onPause");
}
#Override
public void onTagDiscovered(Tag tag) {
IsoDep isoDep = IsoDep.get(tag);
IsoDepTransceiver transceiver = new IsoDepTransceiver(isoDep, this);
Thread thread = new Thread(transceiver);
Log.i("dibawah thread", "ontagdiscovered");
thread.start();
}
#Override
public void onMessage(final byte[] message) {
runOnUiThread(new Runnable() {
#Override
public void run() {
isoDepAdapter.addMessage(new String(message));
Log.i("didlmrun---", "onMessage");
}
});
Log.i("diluarrun---", "onMessage");
}
#Override
public void onError(Exception exception) {
onMessage(exception.getMessage().getBytes());
}
}
hostapduservice :
...
...
...
#Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
if (selectAidApdu(apdu)) {
Log.i("HCEDEMO====", "Application selected====");
return getWelcomeMessage();
}
else {
Log.i("HCEDEMO======", "Received: =====" + new String(apdu));
return getNextMessage();
}
}
...
...
...
then in the manifest file :
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="FEATURE_NFC_HOST_CARD_EMULATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".MyHostApduService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE" >
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="#xml/apduservice" />
</service>
<activity
android:name="de.grundid.hcedemo.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>
</application>
ok,,when i ran the above source code,, i saw my acs122u blinking continously when i tapped my nexus near to it. but i didn't see the log.i(....) from hostapdu service. In the eclipse log cat, there were just some log.i from main activity. what should i do to bind that hostapdu service to main activity, so my nexus can act as a tag...???
thanks in advance... :-)

android: Does not runs on the boot up.... and how to write the same to disable bluetooth

was trying to implement an service that disables bluetooth and wifi when ever user tries to enable bluetooth and Wifi as they are few ways of getting infected.
i have disable use of application use of app locker.
this is what i tried with the tutorials available but so far i have failed to run it on the bootup
Service class
public class MyService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
BroadcastReceiver mReceiver = new StatusReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
}
Reciver
public class StatusReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
wifiManager.setWifiEnabled(false);
}
}
}
Activity
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("HI","I am on activity");
startService(new Intent(this,MyService.class));
finish();
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.active"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" >
</service>
<receiver android:name=".StatusReceiver" >
</receiver>
</application>
</manifest>

Turn off wifi on RINGING phone state

I'm developing an application in which the wifi state goes OFF when the phonestate is RINGING.
My code is as follows :
phonestateManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phone.state"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/ic_launcher" >
<activity
android:label="#string/app_name"
android:name=".PhonestateActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</activity>
<activity android:name=".WifitoggleActivity" />
<receiver android:name=".ServiceReceiver" />
</application>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
PhonestateActivity.java
public class PhonestateActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PhonestateActivity.this,
ServiceReceiver.class);
startActivity(intent);
}
});
}
}
ServiceReceiver.java
public class ServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager);
context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, MyPhoneStateListener.LISTEN_CALL_STATE);
}
}
MyPhoneStateListener.java
public class MyPhoneStateListener extends PhoneStateListener {
#SuppressWarnings("unused")
private WifitoggleActivity ss;
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
break;
case TelephonyManager.CALL_STATE_RINGING: {
ss = new WifitoggleActivity();
Log.d("DEBUG", "RINGING");
break;
}
}
}
}
WifitoggleActivity.java
public class WifitoggleActivity extends Activity {
public WifitoggleActivity() {
System.out.print("INSIDE WIFI");
}
/** Called when the activity is first created. */
private WifiManager wifiManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
} else {
wifiManager.setWifiEnabled(true);
}
}
}
The code is just FORCE CLOSEing in my device. Is it a problem with the Manifest file?
Please help me to find a solution.
Thanks in advance guys.
ServiceReceiver is a BroadcastReceiver not an Activity. You are using wrong code in first block.
Find the onClick handler in your code:
public class PhonestateActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1= (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Then remove these two lines:
Intent intent = new Intent(PhonestateActivity.this,ServiceReceiver.class);
startActivity(intent);
and replace them by
IntentFilter intentFilter=new IntentFilter(Intent.ACTION_ANSWER);
registerReceiver(new ServiceReceiver(),intentFilter);
As ServiceReceiver is not an activity.
Edited
Add one more permission:
android.permission.PROCESS_OUTGOING_CALLS
and change
<receiver android:name=".ServiceReceiver">
<intent-filter>
<action android:name=" android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Now remove two line that registering broadcast from oncreate method
Check this link

Categories

Resources