Can someone please help me out with this.
I want to get notified when my phone battery becomes low.
My androidManifest file looks like this:
<receiver android:name="com.dac.BatteryChangedBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter></receiver>
and my receiver file is:
public class BatteryChangedBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if(Intent.ACTION_BATTERY_LOW.equalsIgnoreCase(intentAction))
Toast.makeText(context, "Battery Power Low or Okay", Toast.LENGTH_LONG).show();
}
}
I am changing the battery level using the telnet command. My phone battery does change but I do not get any toast message. I have even tried registering the receiver using code
You probably need to add the following permission to your manifest file.
<uses-permission android:name="android.permission.BATTERY_STATS" />
The "com. in the android:name looks questionable. If BatteryChangedBroadcastReceiver is in the default package for the application the name should begin with the dot. If not, it should be fully qualified. If you are writing your code in package com -- well -- don't do that, it hurts.
Related
We can hide the android app from launcher by editing manifest XML,but is there any code snippet or example by which we can hide the app and start it by entering some code like ##4444## like that.Any way to do this??
Thanks in advance.
To start your app from dialer you need to do three things:
1. Add receiver to your AdroidManifest.xml
<receiver android:name="com.example.HiddedReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
2. Create BroadcastReceiver as stated in xml. It will intercept EVERY calls number. You just need to scan it for your string and do apropiate action - in this case fire off an intent.
public class HiddenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(android.intent.action.NEW_OUTGOING_CALL)) {
String resultData = getResultData();
if (resultData != null) {
if (resultData.contains("YOURCODE")) {
setResultData(null); // it wont continue calling that number
//HERE CREATE your intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
}
}
3. To get this working, you need to tell android you will be using this feature, and to grant permission to process calls from the user at install time.
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
i didnt test it, but this one works like a charm:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
I have 2 apps: app1, app2
I want to send a broadcast with a permission from app2 to a app1.
In AndroidManifest.xml app2:
<permission
android:name="app2_sendbroadcast_permission"
android:protectionLevel="signature" >
</permission>
Send a broadcast intent:
Intent intent = new Intent();
intent.setAction("app2_sendbroadcast_signal");
sendBroadcast(intent, "app2_sendbroadcast_permission");
In AndroidManifest.xml app1:
<uses-permission android:name="app2_sendbroadcast_permission" />
<receiver
android:name="com.example.app1.App1Receiver"
android:exported="true" >
<intent-filter>
<action android:name="app2_sendbroadcast_signal" />
</intent-filter>
</receiver>
And the receiver of App1 to listen signal from App2:
public class App1Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// String str = intent.
Toast.makeText(context, "Caught signal from sendBroadCast from App2",
Toast.LENGTH_LONG).show();
}
}
However, when I send broadcast from App2, there is nothing would happen. That means I cannot send a broadcast with a defined permission.
Update:
My 2 apps: https://www.dropbox.com/s/j5gseeqgdbz4k0u/Android%20Permission%20Test.zip
Please check and find my issue. Your comments are appreciated
In the source code you shared, Permission name declared in App2 manifest is not matching with uses-permission string in App1 manifest.
Also, while sending broadcast from App2, permission string (2nd param) should be "app1_sendbroadcast_permission"
Intent's action should be "app1_sendbroadcast_signal" as App1Receiver is listening to "app1_sendbroadcast_signal" this action.
If this does not solve the issue, you can try below:
Check if broadcast is received properly without sending second parameter (permission string).
Its always good practice to define action String as com.test.example.APP1_SECURE_BROADCAST. Althugh, this String will not cause any issue.
While declaring permission, Try changing protection level to dangerous.
Check if you are getting any SecurityException.
Correct Fix for this source code:
Please follow proper guideline for defining permission string. Change permission String to something like "com.example.app2.SECURE"
Refer http://developer.android.com/guide/topics/manifest/permission-element.html
It says:
android:name
The name of the permission. This is the name that will be used in code to refer to the permission — for example, in a element and the permission attributes of application components.
The name must be unique, so it should use Java-style scoping — for example, "com.example.project.PERMITTED_ACTION".
Im currently disabling an application via
setApplicationEnabledSetting(String, int, int)
The application is actually disabling itself.
I would expect upon re-installation of the app, the app would re-enable itself, however this isnt the case.
Is there a particular setting required in the manifest to make this work.
(Ive tried setting enabled=true)
Thanks
Im currently disabling all components apart from a broadcast receiver and am trying to catch the installation process to re-enable all the other components again. Which is nasty to say the least
One way to do this is to listen to package installation broadcasts and take action accordingly.
Listen to Intent.ACTION_PACKAGE_ADDED in your broadcast receiver.
If the newly added package is yours, enable the other components.
Example
Manifest:
<receiver android:name =".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
Receiver:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
final String packageName = intent.getData().getSchemeSpecificPart();
if (replacing && "my.package.name".equals(packageName)) {
// Re-enable the other components
}
}
}
}
Hope this helps.
I want to get the package name and class name of the received intent, But am not able to get it.
I want to make my app secure so it asks for password before being uninstalled. Only the user who Installed the app knows the password, so only he/she can uninstall the app.
My code for Receiver:
public class PackageReceiver extends BroadcastReceiver {
# Override
public void onReceive (Context context, Intent intent) {
if (intent.getAction().equals("android.settings.APPLICATION_DETAILS_SETTINGS")) {
/ / TODO:
//I want here to get this getAction working and then I want to fetch package and class of the intent
}
}
}
Manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<Application
android: icon = "# drawable / ic_launcher"
android: label = "Test">
<Receiver android: name = ". PackageReceiver"
android: label = "# string / app_name">
<intent-filter>
<action android:name="android.settings.APPLICATION_DETAILS_SETTINGS" />
<data android:scheme="package" />
</ Intent-filter>
</ Receiver>
</ Application>
Please let me know if I am missing any permission because I can not get this working.
I personally think this behavior is annoying. And for sure it's redundant: there are other mechanisms already on place that tackle the same problem (screen locking, encryption).
I'd only use extra checks when operations are on your side (ie.: delete account, change email, etc).
If I didn't make it to discourage you to do that here's another post that solves the same problem following a similar direction.
When we select a particular app inside the settings screen, a broadcast of the type : android.intent.action.QUERY_PACKAGE_RESTART is fired with package name of the application as an extra. I think you could use that to fire the password dialog.
The receiver code will be something like this :
public void onReceive(Context context, Intent intent) {
String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
if(packageNames!=null){
for(String packageName: packageNames){
if(packageName!=null && packageName.equals("our_app_package_name")){
//Our app was selected inside settings. Fire Password Dialog.
}
}
}
}
I think intent.getExtra("com.android.settings.ApplicationPkgName") should have the package name
i dont know if its acceptable
setting package by getting packagename from the context of activity
intent.setPackage(context.getPackageName());
and to get package name
intent.getPackage()
I want my application to be activated when the user make a specific call. Is there any way to take information which call is making by user in the same time ( not afterwords ) in order to activate the app at the right time ?
Ok i wrote this code for my case and it works:
public class OutgoingCallReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle) return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if( phonenumber.equals("11111111") ) {
Intent myactivity = new Intent(context, MyKeyboard.class);
myactivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myactivity);
}
}
}
In the Manifest i add this:
<receiver
android:name=".OutgoingCallReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
I don't really know, but I think that's a really dangerous practice. It all depends on what you intend to do with your app. If it's a recording app, I think you can't even think of developping it, or don't post on the internet about it because it would be illegal in many countries. That's how I see and how I feel your question. About Android there is not 15k places to search, have a look at the android API.
http://developer.android.com/reference/android/provider/CallLog.Calls.html
This, for example, I don't know if it's a real time log, or if it's filled after the call is terminated. But you can search in that direction.