I would like to be able to open the "Recent Apps" dialog from my application. This is the dialog that is opened by long-pressing the home button. I am programming for Android 4.1 or earlier. I found a way to do it by implementing a custom AccessibilityService and calling AccessibilityService.performGlobalAction(GLOBAL_ACTION_RECENTS), but this requires enabling accessibility on the phone, which is not very desirable. Is there any other way to open this dialog from an app?
Thanks for the help!
This code won't work on Nougat or later
It is possible to trigger the recent apps activity.
The StatusBarManagerService implements an public method which you can use through reflection.
You can use the following code:
Class serviceManagerClass = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClass.getMethod("getService", String.class);
IBinder retbinder = (IBinder) getService.invoke(serviceManagerClass, "statusbar");
Class statusBarClass = Class.forName(retbinder.getInterfaceDescriptor());
Object statusBarObject = statusBarClass.getClasses()[0].getMethod("asInterface", IBinder.class).invoke(null, new Object[] { retbinder });
Method clearAll = statusBarClass.getMethod("toggleRecentApps");
clearAll.setAccessible(true);
clearAll.invoke(statusBarObject);
Have fun
You cannot access that. However, it isn't super hard to roll your own. The getRecentTasks() method returns a list of recently run apps. Simply take the list and add your own UI to it.
One advantage to this is that the default one, at least on older versions of Android, only shows you about 8 apps. If you roll your own can show as many as you want.
This can be done using the TOGGLE_RECENTS Intent.
Intent intent = new Intent ("com.android.systemui.recent.action.TOGGLE_RECENTS");
intent.setComponent (new ComponentName ("com.android.systemui", "com.android.systemui.recent.RecentsActivity"));
startActivity (intent);
Note Package would be changed basis on Api level. See here.
Android 4.1: "com.android.internal.policy.impl.RecentApplicationsDialog"
Android 4.2 - 4.4: "com.android.systemui.recent.RecentsActivity"
Android 5.0 - 7.1: "com.android.systemui.recents.RecentsActivity" ("s" letter was added)
Related
I know a similar question has been asked several times before but I have been through those and they do not help me.
I am trying to set up a Samsung Tab A (Android 8.1) as a single-use device. I have already written the app which works perfectly but as the device will be used by the public I need to lock it down into kiosk mode.
I am trying to follow the developer guide at the link below but immediately hit problems.
https://developer.android.com/work/dpc/dedicated-devices/lock-task-mode
So first thing I try to do is whitelist the app as per the example given:
// Whitelist two apps.
private static final String KIOSK_PACKAGE = "com.example.kiosk";
private static final String PLAYER_PACKAGE = "com.example.player";
private static final String[] APP_PACKAGES = {KIOSK_PACKAGE, PLAYER_PACKAGE};
// ...
Context context = getContext();
DevicePolicyManager dpm =
(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminName = getComponentName(context);
dpm.setLockTaskPackages(adminName, APP_PACKAGES);
The documentation is not clear on where this code should go but I have put it in an override for onResume().
I am using auto-import in Android Studio but it tells me that getContext() cannot be found. I have tried changing to getApplicationContext() and just using this as suggested elsewhere but then it complains that getComponentName() should not have any parameters. I have tried simply removing the context parameter, using context.getComponentName() and also using no context variables. These compile but the app crashes on the device (at setLockTaskPackages()).
I suspect that the key is to find out why getContext() cannot be found and then hopefully everything else falls into place. Any idea what might be missing?
If you're using activity use the keyword this, instead of getContext.
getContext() is used in fragment to get context.Is this is Activity,You can use Yourclass.this or this. also you can get knowladge about what can you use for context using this
In Android 9 media output controls were added to the volume selection dialog.
I would like to have a button in my app which would open these system media output controls so that the user can choose where the audio will be played.
I could find nothing about this in: Android 9 features and APIs, Behavior changes: apps targeting API level 28+ and Behavior changes: all apps
You are looking for MediaRouter API.
public class MediaRouterPlaybackActivity extends AppCompatActivity {
private MediaRouteSelector mSelector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a route selector for the type of routes your app supports.
mSelector = new MediaRouteSelector.Builder()
// These are the framework-supported intents
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
.build();
}
}
For further reference: https://developer.android.com/guide/topics/media/mediarouter#java
Also check Create a MediaRouteSelector section.
I did some research about this as well so I know that Android Pie is capable of this, but they haven't provided a way for us developers to do it yet (at least I haven't found a way). The CATEGORY_LIVE_AUDIO in MediaRouterSelector is also only for secondary output; you can read about it on the developer website. So if you must do it, I think the only way you can do that right now is to do a workaround programmatically:
Disconnect current device (The important of this step depends on the device; I have to do this on my Essential phone)
Use BluetoothAdapter.getBondedDevices() to get a list of paired devices.
Connect to the one you want
Try this - it works for me on Android 11:
Intent intent = new Intent("com.android.settings.panel.action.MEDIA_OUTPUT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
In Android 6.0/API23 and earlier, the following used to work:
String settingEnabled = android.provider.Settings.Secure.getString(this.getContentResolver(), "enabled_notification_listeners");
In Android 7.0 Nougat/API24 this seems to be no longer supported, because the code above returns null.
It actually was never mentioned here: https://developer.android.com/reference/android/provider/Settings.Secure.html
How do we check if our app has notification access in Android 7.0 Nougat API24?
Edit: It seems that actually that after you first gained the access in the settings, the code above returns the correct state. But not on the initial request after installation.
Use this:
Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages (context);
in agreement with the recent post from Android Developers http://android-developers.blogspot.pt/2013/10/getting-your-sms-apps-ready-for-kitkat.html ,I was trying to prepare my app to the new android version, but encountered a problem with the part they suggest to create a dialog to let the user set the app as the default application to handle SMS's :
Android Developers Post
public class ComposeSmsActivity extends Activity {
#Override
protected void onResume() {
super.onResume();
final String myPackageName = getPackageName();
if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
// App is not default.
// Show the "not currently set as the default SMS app" interface
View viewGroup = findViewById(R.id.not_default_app);
viewGroup.setVisibility(View.VISIBLE);
// Set up a button that allows the user to change the default SMS app
Button button = (Button) findViewById(R.id.change_default_app);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent =
new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
myPackageName);
startActivity(intent);
}
});
} else {
// App is the default.
// Hide the "not currently set as the default SMS app" interface
View viewGroup = findViewById(R.id.not_default_app);
viewGroup.setVisibility(View.GONE);
}
}
}
the code itself in pretty much straightforward, but I'm unable to access to Telephony.Sms.getDefaultSmsPackage because it says that Telephony cannot be resolved, and I can't find any import or declaration that would fix that.
Can anyone please help?
android.provider.Telephony simply doesn't exist yet (as of API 18 - 4.3).
This class will be added in 4.4 (presumably API 19), and that blog post is highlighting the changes that you should make once the new API is released so you aren't surprised when the time comes.
From the end of the post:
To help you make the changes, we'll soon be providing the necessary SDK components for Android 4.4 that allow you to compile and test your changes on Android 4.4.
Don't forget that you should wrap this code in an API version check so you don't run into issues with older versions that don't have this class.
this change will break all the SMS blocking apps.
"Note that—beginning with Android 4.4—any attempt by your app to abort the SMS_RECEIVED_ACTION broadcast will be ignored so all apps interested have the chance to receive it."
Do you think there is a way to go around this?!
Maybe at least on Root?
Apparently there is with root access. The latest version Cerberus app claim to be doing this.
Now, if only I knew how they do it :(
I'm using the book "Embedded Android".
I'm making a new System Service using AOSP(4.0.3_r1).
I want my system service to be registered in frameworks/base/core/java/android/content/app/ContextImpl.java so that I can use it through getSystemService() method.
The problem is, I can't find the app folder under content:androidroot/frameworks/base/core/java/android/content/app/ContextImpl.java
But, I found it in:androidroot/frameworks/base/core/java/android/app/ContextImpl.java
Are these 2 files the same? or is it just missing(the content/app folder)?
Any idea on what to do?
Karim wrote his book mostly orienting on Android 2.3.4 version. Something can be changed from this time. This is an example what has been changed.
Are these 2 files the same? or is it just missing(the content/app folder)?
These are the same files.
Any idea on what to do?
As I said the implementation has been changed. I looked into the code and here what you can change to make your code working (I can only suppose because I did not actually build my code). In the static block of ContextImpl class you need to add the following code:
registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(OPERSYS_SERVICE);
IOpersysService service = IOpersysService.Stub.asInterface(b);
return new OpersysManager(service);
}});
You need to use SystemServer which holds all system services' names.
You should check this link out:
http://processors.wiki.ti.com/index.php/Android-Adding_SystemService