I am writing an Android app which must always be running in the background until the user exits it from the app's menu. But now I notice that in Android after some time my app is stopped by itself without user intervention.
Any idea how to make sure my app will always be running in the background?
You need to run a Service of your own.
http://developer.android.com/reference/android/app/Service.html
If you need to run at all times, look into a Service and startForeground. If you can let your Service die but get restarted, look into onStartCommand and START_STICKY.
AndroidMainfest.xml looks like this with presistent=true:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="7" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:persistent="true">
<activity
android:name="com.example.test.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
“While the app development documentation does explain the role of
android:persistent, the use of that attribute is reserved for apps that
are built within the AOSP.”
– Embedded Android
For your application, in the manifest xml, put:
android:persistent="true"
Related
I'm currently learning android using the book from Big Nerd Ranch. Right now I'm creating an app where I recently changed the startup activity. Now the problem is, that whenever I run the application from my IDE (IntelliJ) either on my phone or my AVD, it starts the wrong activity (The one it used to start on before i changed it). However, if i open the app by clicking on the icon on the phone, it starts the right activity.
I want it to start using the activity "CrimeListActivity". Here is my manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.android.criminalintent"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<application android:label="#string/app_name">
<activity android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".CrimeActivity"
android:label="#string/app_name">
</activity>
</application>
Maybe check the default Activity inside Android Studio
take a look
I am making an app that communicates with a piece of usb hardware made by my company (this is the only app allowed to talk to the usb accessory, it's not a public api). I am having difficulties setting up the proper launch modes in the manifest.
There are three components to the app: the main activity, a login activity, and the USBService.
I'm assuming the intent for the main goes to the login activity, and the intent for the usb goes to the USBService, but I am not sure if I do this, will this start the service if the app is not running? More over, if it does, how do I fetch an already existing service?
What type of structure should I be looking at for the manifest file? (specifically, intent-filters, and appropriate launch modes... I've read a few documents about the launch modes but I am still not sure I quite understand... There should only ever be at most one instance of each activity/service, and they need to communicate together.
edit: it is not necessary for communications to start before the app is open, nor is it necessary to launch the app automatically when the usb is connected.
edit: my manifest as it stands, looks like:
<uses-feature android:name="android.hardware.usb.accessory" />
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="mainpackage.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="#xml/accessory_filter" />
</activity>
<activity
android:name="mainpackage.LoginActivity"
android:label="#string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<service android:name="updater.USBService"
android:exported="false" >
<!--
-->
</service>
</application>
in your manifest add
<manifest ...>
<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk android:minSdkVersion="12" />
In this case, the following resource file should be saved in res/xml/device_filter.xml and specifies that any USB device with the specified attributes should be filtered:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="1234" product-id="5678" class="255" subclass="66" protocol="1" />
</resources>
Hope this help.
Your manifest looks good,
I think you make a good choice for putting the intent-filter "android.hardware.usb.action.USB_ACCESSORY_ATTACHED" in the mainActivity and start the application in this activity,
and again I think it's a good choice to start your mainActivity in SingleTop launch mode,
because if an instance of the mainActivity already exists at the top of the current task, the system going to launch this activity, no new instance of this activity will be created.
For a best understanding of the different launch mode available in android,
I think this link may help you :
http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
To make a long story short I think you'll be all set with this manifest as is.
To Use Android Devices min SDK version should be set to 12 and need to declare following line in AndroidManifest.xml file
<>
<uses-sdk android:minSdkVersion="<version>" />
...
<application>
<uses-library android:name="com.android.future.usb.accessory" />
<activity ...>
...
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="#xml/accessory_filter" />
</activity>
</application>
I have installed the Android App on which I am currently working on my ASUS tablet, running 4.0.3
However, I must have accidentally deleted something in the Manifest.xml, because there is no words below the app icon. In other words, the app name is not appearing below the app icon on my device, and, as such, it is sorted in the All Apps screen in the wrong place.
Here is the relevant section of my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tra.games.mytaboo"
android:versionCode="1"
android:versionName="1.2.1" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="Taboo"
android:theme="#style/AppTheme" >
.....
What am I missing?
Tim, Label of your launcher activity will be displayed.
<activity
android:label="Taboo"
android:icon="#drawable/icon"
android:name=".activity.LaunchrActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
http://developer.android.com/guide/topics/manifest/activity-element.html#label
<activity
android:label="Your Name" <---- here name!
android:icon="#drawable/icon"
android:name=".activity.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
You could call the setTitle() function from within your Activity. It takes either an int (resource ID) or a CharSequence as a parameter.
You have missed android:label property. which is set your application name under the icon
android:label="#string/app_name"
Here I have set to load app name from string.xml file.
You can set it directly like this too
android:label="app name"
I have a basic clock widget here, it works just fine on versions of android below 4.0, you can long press, select widgets and its right there. But when i try to run it on 4.0 or later emulator or real device, it does not show up in widgets section only in Settings>Storage>Apps. I have tried adding a simple activity that just gives users directions on how to install the widget , that suggestion was given as an answer here: Android 4.0: widgets not appearing? . Unless i did it wrong the widget still does not show up in widget drawer.
Here is my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widgetexample1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ExampleAppWidgetProvider"
android:label="8-bit cloud widget 1">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/widget1_info" />
</receiver>
</application>
</manifest>
add below in your manifest.xml.
"android:exported="true".
it might help.
try it.
In your widget metadata xml file check tag <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:xmlns attribute should start with http://.
It looks like you are being hit by a security upgrade which I think happened in HoneyComb.
Basically, in newer versions of Android, you cannot have your widget working, without first launching an Activity.
So add an activity to the project, run it first, and I think your widgets will update.
A good example would be to use a Settings style activity, or perhaps and About box type of thing, to tell the user a bit about your widget. (I use settings in my widgets).
For more info:
Android 4.0: widgets not appearing?
I'm having an issue starting activities in order, and I don't know if it is an issue in the manifest or in the code. I tested this code a while ago when it was working, but now it's not.
The first activity links to the second, which links to the third. I listed the first activity first in the manifest. However, when I start my emulator, it's the second activity that runs first. I am very confused. Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hmdywifinal.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Activity1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2"
android:label="Startpage">
</activity>
<activity android:name=".Activity3"
android:label="Activity3"></activity>
</application>
Do you think something is wrong with it?
Make sure you are running your program from Activity1 and not from Activity2.
If you run it from Activity2 it will skip Activity1 even though you have your manifest set like you described above.
Order in which Manifest file declares Activities has nothing to do with the runtime order.
First Activity gets launched from a Launcher ( which is Activity1 in your case )
I am assuming you are launching Activity2 and 3 using Intents in your code. So you are in control on the way these activities get launched.
Refer to the api Demos, Which has got a similar application Proof Of Concept. It will give you a better idea on mving from one application to other.