I wrote an app, which shows a dialog when charge lvl is 15% or low.
Problem is that on one forum I received the message that it does not work on Google Nexus S (I9023). MIUI 2.3.7c.
code for receiver is (it is registered in manifest):
if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW))
{
try
{
context.startService(myIntent);
}
catch(Exception ex){Toast.makeText(context, "Problem with Starting a Service", Toast.LENGTH_LONG).show();}
}
In service I register receiver with filter Intent.ACTION_BATTERY_CHANGED.
code for service in is:
...
if (batteryLevel == mLowLevelAlarm)
{
if (prefView.getBoolean(PREF_LOW_CHARGE_ON, true))
{
myIntent = new Intent(MyService.this, BatteryLow.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
}
}
...
Can anyone help to solve this problem?
thx
Related
I want to start service only when device is unlocked. I've tried this code but it didn't work.Can somebody explain me what do I do wrong?And what should I do?
if( myKM.isKeyguardLocked()==false) {
startService(intent);
Log.i("Point ","5");
} else {
Log.i("Point ","6");
stopService(intent);
}
You can have a Broadcast Receiver for ACTION_USER_PRESENT
No you can check in onReceive (Context context, Intent intent) as below -
if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
// device unLocked
}
I'm currently trying to get broadcast receivers running in the background of my android application, which I've been told to use an event service for. At present my broadcast receivers work fine if you're in the activity within which they're registered
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {
Toast.makeText(arg0, "Battery's dying!!", Toast.LENGTH_LONG).show();
Log.e("LOW", "LOW");
intent = null;
}else if(intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
Toast.makeText(arg0, "Battery's discharging!!", Toast.LENGTH_LONG).show();
Log.e("discharge", "discharge");
intent = null;
}else if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
Toast.makeText(arg0, "Battery's charging!!", Toast.LENGTH_LONG).show();
Log.e("charge", "charge");
intent = null;
}else if(intent.getAction().equals(Intent.ACTION_BATTERY_OKAY)) {
Toast.makeText(arg0, "Battery's okay!!", Toast.LENGTH_LONG).show();
Log.e("OKAY", "OKAY");
intent = null;
}
}
};
in OnCreate:
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_LOW));
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_OKAY));
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_POWER_DISCONNECTED));
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_POWER_CONNECTED));
Despite the issues that arise with leaking intentfilters by making them in oncreate, my current issue that that when I change activity these no longer run, I've read from the following: http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html#startingservices_alarmmanager
That by placing these into an IntentService and starting this service, they'll run consistently, however this involves registering the receivers in the manifest, which gives me issues in that my application is planning on allowing the user to listen out for specific events IE: these cannot be created dynamically.
Is there a way to dynamically create broadcast receivers within a class, which runs in the background of the application and is triggered when the broadcast occurs?
In my application, i start another application (not activity) with this code :
protected void launchApp(String packageName) {
Intent mIntent = getPackageManager().getLaunchIntentForPackage(
packageName);
if (mIntent != null) {
try {
startActivity(mIntent);
} catch (ActivityNotFoundException err) {
Toast t = Toast.makeText(getApplicationContext(),
"App not found", Toast.LENGTH_SHORT);
t.show();
}
}
}
but i would like this application (launched by packageName) run in background and not disturb the UI.
Is it possible ?
Thanks!
you can use broadcast receiver in target app and start it with broadcast
Hope it will help
you can use this
startActivity(getPackageManager().getLaunchIntentForPackage("com.example.appName"));
further information please see this link
Android - How to start third party app with package name?
I'm now trying to transfer files between Android devices via bluetooth. I already implemented my sender side. I'm not using InputStream/OutputStream. I'm using Intent.ACTION_SEND. Everything in there sender side works fine, but when it comes to the receiver side, I'm facing two problems.
There's the pop out notification saying "Do you want to receive this file?". Is there any way I can avoid this thing?
How can I know that there's a file coming in and the file transfer is finished or stopped at the receiver side?
It seems that these two problems can be solved using InputStream/OutputStream, but I don't really want to use them. Maybe a listener that monitors Bluetooth, or some functions in BluetoothAdapter/BluetoothDevice can do this?
Thanks for help. My code is like below: (in my MainActivity.java)
public void beginBT() {
if (isSender) {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/log.txt");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
if (!findBluetoothForIntent(intent)){
Toast.makeText(this, "Bluetooth Not Found.", Toast.LENGTH_SHORT).show();
} else {
//intent will send the file via bluetooth
startActivity(intent);
}
} else { //receiver side
//make device be discoverable
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
public boolean findBluetoothForIntent(Intent intent){
List appsList = getPackageManager().queryIntentActivities(intent, 0);
String packageName = null;
String className = null;
for (Object info: appsList){
if (info instanceof ResolveInfo) {
packageName = ((ResolveInfo) info).activityInfo.packageName;
if (packageName.equals("com.android.bluetooth")){
className = ((ResolveInfo) info).activityInfo.name;
break;
}
}
}
if (className != null) {
intent.setClassName(packageName, className);
return true;
} else {
return false;
}
}
Answering my own question is always so much fun!!
About the pop out notification, I can't do anything about it, unless I use InStream/OutStream.
For the receiver side, use BroadcastReceiver to monitor actions of the device. Here I monitor the disconnecting action of bluetooth. Because there will be a connecting action when the device begin to receive file, and when it finishes, there will be a disconnecting action.
Don't know if the following code would help anybody, :)
MainActivity.java
private static final String BT_DISCONNECTED = "android.bluetooth.device.action.ACL_DISCONNECTED";
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == BT_DISCONNECTED) {
//now file transmitting has finished, can do something to the file
//if you know the file name, better to check if the file is actually there
// - make sure this disconnection not initiated by any other reason.
}
}
IntentFileter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter);
Warning: remember to unregister this receiver when you exit the activity, or simply when you don't need it
I need to handle the connectivity change broadcast in my app. Every thing is great except that when it comes for the broadcast the application crashes. I am using the following code in my Broadcast :
#Override
public void onReceive(Context context, Intent intent) {
Log.i("NET", "Broadcast started");
Intent startServiceIntent = new Intent(context, NewsService.class);
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if(noConnectivity) {
context.stopService(startServiceIntent);
Toast.makeText(context, "Connection is terminated!", Toast.LENGTH_LONG).show();
Log.i("NET", "Stopped");
}
else {
context.startService(startServiceIntent);
Toast.makeText(context, "Connection is ok!", Toast.LENGTH_LONG).show();
}
}
This code is supposed to stop a service when no internet connection is found and to start it whenever it finds a connection.
Lastly and after 2 days of searching I found the problem, it is about the package the broadcast is placed in I found it here