Add subview to arbitary activity android - android

I'm going to show notification in any view in my Android application, but I didn't find any way how to do it without getting current activity.
On iOS we can just do
Window.AddSubview(customNotification);
But is there any way to do it in Android?

When you refer to notifications in Android, it means the notifications you can see out side the app (see notifications documentation for an explanation). I assume this is not what you are referring to because you are referring to the iOS Window.
Android also has a Window class. However, an Activity is usually the right UI component you should be looking at when adding a custom View. As opposed to iOS, an Android app almost always needs to work in a certain Context to add UI components and you can't just add one to a static Window.
My suggestion is then to change the logic a bit. I assume you have a running Activity at any point (if otherwise and you are running from a Service, perhaps notifications are the right solution after all). So what you can do is to broadcast your request to show certain data and the active Activity will pick it up and add your custom view to itself in the right spot.
Another option is to start a new dialog Activity using the application context.

Related

Display a Toast message when Android for Work configuration is missing

I'd like to prevent my application from starting when an Android for Work profile is not available for my application (not yet configured, or deployed on the device). Instead, I'd like to be able to display a Toast like message telling the user to contact his IT administrator. Example of this at the bottom of this message.
Example:
Divide Productivity Suite of application displays this message (mail, notes, etc).
"Configuration from managing application required. Contact your IT admin for details".
Screen Capture
Is there a way to implement this? I've tried to hook into MainActivity onCreate function, or even put it directly in the Application onCreate() function. Hooking code in here seem to still have launched the application (the title bar is displayed despite there's no content displayed).
I was able to figure out how to determine if your Application was running on a for Work profile and display a alert dialog here:
Android for work - How to check if my application is running in the work profile?
Man i can give you specific topic . You can search for services instead of Activities. Activity codes performing only when application is running .
But you can call services everytime without your app is not running.
You can look here
Apparently, this was actually pretty trivial.
In your AndroidManifest.xml, you reference a new Activity that starts your main application. This activity will use the "Theme.Translucent.NoTitleBar"
When you are unable to configure Android for Work profile, use a Toast.makeToast() message to notify your user, then call finish() and return from your onCreate() function.
When you are able to complete Android for Work configuration, start your MainActivity by creating an Intent, set the action and category and then call startActivity() from your initial Activity.

App inside an app

Is it possible to run an application from inside another application? What I want to do is write an app which allows you to chose an app to start, and then displays the activities of this app inside a view.
So in landscape mode, it should look something like this:
The idea behind this is:
I want to be able to start and run a third party activity next to my own activity, and I want to be able to create individual makros with my activity that are controlling the third party activity.
Basically, something like this:
Start third party activity from inside my app
Start makro recording
Do something in third party activity
Stop makro recording
Use makro whenever you wish
So how can I start and control another activity from inside my own activity?
Unrooted:
Sadly, what you want to achieve does not seem to be possible without rooting the phone, because you can only interact with other apps via intents. Since developers decide how their apps react on specific intents, creating macros this way is nearly impossible.
With rooted phones:
You may want to create a list of all installed apps, you can use
getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
to retrieve a list of all installed apps.
If the user now selects an app, launch it via an intent and create a system overlay to get all touch/key events (and let the user stop the macro). You can find a way to do this here. Store the x/y-values of the touch-events.
You can recreate the events using MotionEvent#obtain.
Now comes the part where you need a rooted phone (the permission INJECT_EVENTS). Launch the app and inject the events so your macro gets executed. Samplecode:
Instrumentation m_Instrumentation = new Instrumentation();
m_Instrumentation.sendPointerSync(motionEvent);
You can find more information about injecting (also keyevents) here.
If you need help to compile your app, these 2 links will help you: How to compile Android Application with system permissions, Android INJECT_EVENTS permission
It's not possible to start an application in a View, but you can launch an app from within your app:
Intent i = getPackageManager().getLaunchIntentForPackage("com.package.ofapp");
startActivity(i);
//EDIT to your updated question:
After starting the activity from the above code, one way you could start/stop the macro at any time in the new app would be to create a small view overlay on top of the screen.
This overlay would be on top of ALL activities.
Check out the following link: Creating a system overlay window (always on top)
You could write code to start the macro when the View is pressed, and then if the button was pressed once and the user presses it again, stop the macro. This would be in the onTouchEvent() method.
Yes, I think it's possible as a app named floating apps does that (WITHOUT ROOT)
Only using some adb commands
https://play.google.com/store/apps/details?id=com.lwi.android.flapps
Yes its possible if you use Intents. They allow you to move between screens and to launch another different functionality inside the same app. visit coursera for more tutorials on intents

android access widget list, coax/remind users to install widget

I would like users to install the widget component of my app. Currently my "mainactivity" simply pops up a textview saying that there is a widget and to see their widget list.
Unfortunately right now, this requires them to open the mainactivity at all. If they want to see the widget in their list of widgets.
I don't want to do anything annoying, but there does seems like there are a lot of hurdles to actually getting and using a widget right now.
Things I could do: setup an onboot service that checks to see if the widget is on the launcher screen. It could remind users once or twice (ever) via some kind of notification. The widget can turn off that service using its onUpdate method or other lifecycle commands. I personally hate erroneous notifications.
One thing I'd like to do is programmatically open up the widgets list. is that possible? any other best practices? Since we can't have widget only apps anymore, I'd still like the main component of my app to be a widget.
setup an onboot service that checks to see if the widget is on the launcher screen
That will require the user to launch your activity, anyway, on Android 3.1+. Your "onboot service" will not run before then.
One thing I'd like to do is programmatically open up the widgets list. is that possible?
No. After all, the user has to indicate where on the home screen the app widget goes first.
any other best practices?
An app that is purely an app widget, unless it is blindingly obvious that it is only an app widget, is going to have these sorts of issues. That is why many app widgets are simply one piece of a more substantial app, so that if the user elects not to use your app widget, or does not notice that it is there, it is not that big of a deal.

Screen to show app output inside app in android

I'm writing an android app just to get familiar with the OS and API. I'd like to show what my app is doing to the user in a seperate screen and not via notifications. The reason is that I'd like to show the user exactly what the app is querying and how long it took etc. I saw an example of this in the superuser app which shows what the app is doing when it is updating itself.
Is there a library to do this?
Is it as simple as just showing a text box and then populating it programatically?
I would have done it so..
Just push state changes to your UI thread and display them there.
You can do that via BroadcastReceiver, which is intended to let background services communicate with UI activities, or LocalBroadcastManager which is simpler version of the previous.
Here on StackOverflow you will find dozens of questions describing how to implement it.

How to make widget of running application in android?

I want to make my running application's widget. I mean I want to access my application through widget on home screen. I want to dial a call, send sms to particular number which is stored in my application.
Is it possible in android? I am stuck on this if anyone has any type of idea then please suggest me..
Edit:-
I want to make widget like Power Control. In this we can operate all features of settings through widget. At the same ways I want to control my application through widget and for that I have to sync my application with widget or visa-versa. So I am not able to understand how can I do that.. Can anyone help me in this manner?
Thanks.
You can create broadcast receiver to consume all the broadcast events, and it doesn't matter if you fire broadcast events from your application of from your widget, they all will be handled at your broadcast listener.
There is no shortcut to convert an app to widget.
Sorry buddy.
You should do an additional component to your application. That component will be widget. In Eclipse you could add a new class to your package through the wizard and say that it will be a widget.
It should have simple layout - check elements you used in the view. only FrameLayout, LinearLayout, RelativeLayout, AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView, ViewFlipper are allowed. Using forbidden elements causes "Problem Loading Widget" message. Beware!
I think, you have already seen that page about how to place a widget in Manifest and how to make an appwidget-provider.
After installing on the device or emulator you whould install widget to the screen. Long touch will conduct you to the widget install wizard of the device/emulator. You will need to do it only once - at every debug launch you'll have the new version on the right place automatically.
Don't forget to renew all important views though - it will not be done automatically. (check what is not refreshing by itself and refresh them in code).
I think, you already know, how to call your launch Activity from the widget body. Only now it won't be launch activity already, but the activity to be first launched from the widget. The widget will be declared as launch in the Manifest.
System settings: just localize your settings-modifying logic in one class (like PowerControlHelper or something), figure out what parameters your methods will take and reuse it both in widget and in full-UI application. Not much difference there, you can access all the same android APIs from within widget logic as well as from activity logic.
Your application's own settings: it doesn't matter where you access your private data as long as it's the same application. See http://developer.android.com/guide/topics/data/data-storage.html#pref for more details on Shared Preferences.

Categories

Resources