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
Related
I would like to use a broadcastReceiver to catch the headset button click with my android application but it doesn't seem to pass into my receiver class. I looked a large part of questions yet, but no working answers. Thank you.
PS : Sorry for my english ;)
Here's my mainActivity :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
HeadsetReceiver r = new HeadsetReceiver();
filter.setPriority(1000);
registerReceiver(r, filter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My BroadcastReceiver class :
public class HeadsetReceiver extends BroadcastReceiver {
private final String ACTION_BUTTON_HEADSET = "android.intent.action.MEDIA_BUTTON";
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTION_BUTTON_HEADSET)){
System.out.println("Just a test");
}
}
}
And my manifest :
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver class="mypackage.HeadsetReceiver"
android:name="mypackage.HeadsetReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
You also have to register your receiver the audio manager. You can do it in your onCreate of your activity:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
audioManager.registerMediaButtonEventReceiver(
new ComponentName(this, HeadsetReceiver.class));
Also you can delete the IntentFilter code, as it is defined already in your manifest.
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.
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.
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>
i have problem with sending Broadcast receive from one activity to other ..its not working my code is below..pls refer to it ..
sending class is:
public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST";
/** 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 View.OnClickListener() {
public void onClick(View view) {
sendBroadcast();
}
});
}
public void sendBroadcast(){
Intent broadcast = new Intent();
broadcast.setAction("com.unitedcoders.android.broadcasttest.SHOWTOAST");
sendBroadcast(broadcast);
}
}
and reciving class is :
public class ToastDisplay extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("asdasd","sdasdasd");
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "%%%%%%%%%%%%received", Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(SendBroadcast.BROADCAST_ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
}
Manifest file is ::::
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unitedcoders.android.broadcasttest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SendBroadcast"
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=".ToastDisplay">
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"> </action>
</intent-filter>
</activity>
</application>
</manifest>
you have not registered your Receiver in Manifest :registered as
<receiver android:name="receiver">
<intent-filter>
<action
android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>