I have following permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Then I have this BroadcastReceiver:
public class StartAfterRebootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Properties.DEBUG) Log.d(TAG, "Receiver started");
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.registerNetworkCallback(
builder.build(),
new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
if (Properties.DEBUG) Log.d(TAG, "Receiver running after connectivity up");
if (Properties.DEBUG)
Log.d(TAG, "Receiver starts service, probably once");
if (!UpdateService.isCurrentlyRunning()) {
if (Properties.DEBUG)
Log.d(TAG, "Receiver UpdateService is not running, try to start");
UpdateService.startService(context);
}
}
#Override
public void onLost(Network network) {
if (Properties.DEBUG)
Log.d(TAG, "Receiver not running after connectivity lost");
if (UpdateService.isCurrentlyRunning()) {
if (Properties.DEBUG)
Log.d(TAG, "Receiver UpdateService is running, sometimes need to redesigned stop service");
UpdateService.stopService(context);
}
}
}
);
}
}
I have Android TV box with Android 11, exactly I have two boxes from different vendors, on both is there only ethernet, not wifi, but on one of them is all working well, but on the other app start, starts foreground service correctly, when I plug out ethernet cable, service is correcty destroyed, but its all. App dies and over logcat I got this message:
05-18 22:05:03.444 548 852 D ConnectivityService: ConnectivityService NetworkRequestInfo binderDied(NetworkRequest [ LISTEN id=19, [ Capabilities: NOT_RESTRICTED&TRUSTED&NOT_VPN&FOREGROUND Uid: 10027 AdministratorUids: [] RequestorUid: 10027 RequestorPackageName: com.droidlogic.updater] ], android.os.BinderProxy#dc6b02d)
What I am doing wrong. I am sitting here over week and I cant see any mistake. Its system app compiled with android.
Thank you very much.
D
I moved ConnectivityService to Service and it helps.
Related
I'm new to the Android platform and I want to create an app that creates a wi-fi hotspot so my microcontroller (esp32-c3) can connect to it. Further, the app should only contain a webview and be able to automatically determinate the IP-adres of the microcontroller so there can be established a Websocket connection to receive sensor data.
After much frustration setting up the IDE, I finally managed to build a simple app and and run it on my phone. My problem is that I always get a failure with reason code 0 (WifiP2pManager.ERROR) after creating a group, which is according to the documentation an internal error. I've tested this on multiple phones (Android 12 and 13) with the same result.
What I noticed is that there are barley examples available covering this topic. Am i making a mistake and is it possible to find out the detailed error message instead of seeing just htis generic error code?
This whole platform is difficult and frustrating. Can someone help me out? It must definitely be possible to create this on my phone, because with the app PdaNet+ I'm able to create a wi-fi direct hotpot that works.
public class MainActivity extends AppCompatActivity implements WifiP2pManager.ChannelListener, WifiP2pManager.ActionListener {
private final IntentFilter intentFilter = new IntentFilter();
private TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = this.findViewById(R.id.tv1);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), this);
manager.createGroup(channel, this);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
tv1.append("\n" + action);
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
tv1.append("\nWifi P2P is enabled");
} else {
tv1.append("\nWifi P2P is disabled");
}
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
tv1.append("\nCall WifiP2pManager.requestPeers() to get a list of current peers");
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
tv1.append("\nRespond to new connection or disconnections");
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
tv1.append("\nRespond to this device's wifi state changing");
}
}
};
registerReceiver(receiver, intentFilter);
}
#Override
public void onSuccess() {
tv1.append("\nGroup created successfully");
}
#Override
public void onFailure(int reason) {
tv1.append("\nFailed to create group (reason "+reason+")");
}
#Override
public void onChannelDisconnected() {
tv1.append("\nonChannelDisconnected");
}
}
Manifest:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I finally solved the problem: it turned out the location permission was blocked. After giving access, the hotspot could get created.
I am trying to scan beacons even when I quit my app using Service.
This is my code for the service.
public class MyService extends Service {
private static final String TAG = "MyService";
BluetoothManager bluetoothManager;
BluetoothAdapter bluetoothAdapter;
BluetoothLeScanner bluetoothLeScanner;
public MyService() {
}
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate called");
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter =bluetoothAdapter.getDefaultAdapter();
bleCheck(bluetoothAdapter);
bluetoothLeScanner.startScan(mScanCallback);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand called");
if (intent == null)
return Service.START_STICKY;
else{
}
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy called");
}
And these are the permissions i gave to app. I post all the permissions in case there might be problems.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
I used Wake_lock but it stops when the screen turns off. I cannot find the reason. And another question is that even though my phone OS is Android 9.0 the background service does not stop.
In the google document, I read that after Android Oreo, they strictly restrict app running on background but my app keeps on running.
Thanks for reading my question. Please help:)
I need a Broadcast Receiver name "NetworkReciver.java" that should execute when Internet is Connected or Disconnected. But it is not executing when app is closed or when app is not running.
API 23 and Above
NetworkReciver executes only when app is running.
I want a reciever to monitor the Internet Connection even when the app is closed.
Manifest permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
In Activity
this.registerReceiver(mYBroadCastReceiver, new IntentFilter(
"android.net.conn.CONNECTIVITY_CHANGE"));
NetworkReciever.java:
public class NetworkReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.i("TAG", "Network REceiver Connection Chnaged");
}
}
I have a real android device (Android 4.1.1). Which supports applications like Xender, ShareIt, Zapya etc which uses WiFi Direct to transfer files.
But when I run my application, it says P2P_UNSUPPORTED.
My manifest permissions are....
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
My MainActivity.java is like this..
intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
Receiver receiver=new Receiver();
registerReceiver(receiver,intentFilter);
WifiP2pManager manager=(WifiP2pManager)getSystemService(WIFI_P2P_SERVICE);
channel=manager.initialize(this, Looper.getMainLooper(),null);
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
// Code for when the discovery initiation is successful goes here.
// No services have actually been discovered yet, so this method
// can often be left blank. Code for peer discovery goes in the
// onReceive method, detailed below.
}
#Override
public void onFailure(int reasonCode) {
// Code for when the discovery initiation fails goes here.
// Alert the user that something went wrong.
switch (reasonCode){
case WifiP2pManager.BUSY:Log.e("code","BUSY");break;
case WifiP2pManager.ERROR:Log.e("code","ERROR");break;
case WifiP2pManager.P2P_UNSUPPORTED:Log.e("code","P2P_UNSUPPORTED");break;
}
}
});
In the Log,, I get.
code﹕ P2P_UNSUPPORTED
And my BraodcastReceiver is like this..
#Override
public void onReceive(Context context, Intent intent) {
//Log.e("intent",intent.getAction());
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
Log.e("raw state", ""+state);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Log.e("state", "ON");
} else if(state == WifiP2pManager.WIFI_P2P_STATE_DISABLED) {
Log.e("state", "DISABLED "+ state);
}
}
And there i get the log as,,
raw state﹕ -1
NOTE : the variable state neither equals WifiP2pManager.WIFI_P2P_STATE_ENABLED nor WifiP2pManager.WIFI_P2P_STATE_DISABLED
It is possible that the applications that you have listed are able to use alternative transports other than Wi-Fi Direct. The P2P_UNSUPPORTED error implies that your device does not have the Wi-Fi Direct feature. You can try confirming this by executing the following: getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT). Alternatively, if your device has Wi-Fi Direct, you can access a Wi-Fi Direct settings activity from the Wi-Fi settings of your device.
I want to receive notification when Mobile network connection is lost or received. Through following code, I can receive notification for Wi-Fi (Data) connection but not for Mobile (Voice) connection.
Manifest :
<uses-permission android:name="android.permission.ACCESS_NETWORK_ST ATE"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".notifier">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE " />
</intent-filter>
</receiver>
</application>
Java :
public class notifier extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast toast = Toast.makeText(context, "Network Changed !!", Toast.LENGTH_LONG);
toast.show();
}
}
Please tell me, how can I receive notification for Mobile Network (Voice).
Pseudocode!!
PhoneStateListener myListener = new PhoneStateListener() {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
// Handle different service states here.
}
};
((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE))
.listen(myListener, PhoneStateListener.LISTEN_SERVICE_STATE);
http://developer.android.com/reference/android/telephony/TelephonyManager.html#listen(android.telephony.PhoneStateListener, int)
Check the permission in the AndroidManifest.xml, first.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
And try this code below.
ConnectivityManager manager = (ConnectivityManager)appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(mobile.isConnected()) {
//Do Sth.
}
If this is not work, check your phone's brand. If the phone's developer blocked some codes or not implement some code, it doesn't work. What is your phone?