Is it possible me to start my android application if the user connecting USB cable to the device ?. I am going through the this link . Am I in the correct path ?
Register an Receiver for ACTION_POWER_CONNECTED in Manifest as:
<receiver android:name=".OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
and in Code part
public class OnPowerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Your_Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Yes, you can do it, you need to code for a BroadcastReceiver that will get fire when any event occurs at USB port ( plug-in or plug-out ).
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
when such things occurs, just fire an Intent to start your activity.
Related
I want to run an application automatically when the phone reboot. I have added these lines to my code:
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyActivity.class);
context.startService(serviceIntent);
}
}
And I have added these lines to my Manifest :
<receiver
android:name=".BootComplete"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and it's permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
It works properly when MyActivity class contains just some background tasks. For example when I want to turn on a LED. But when I want to play a media (ex. a video) it doesn't work. Just says service has started and doesn't play anything.
How can I fix it? Is there anything that I must add to the code?
The problem is not with your code! It's about android device.
When the device boot up, you receive the broadcast. But pay attention, when you receive that broadcast, the SD CARD is still populating and you can't access it. If that media is on SD card you cant play it.
Also, you should pay attention to something else: if the user install your app in his/her SD card, that broadcast receiver would never trigger.
So, if you want to prevent problem in some devices, in your manifest, choose internal for your install location too.
Do this change in BroadcastReceiver:
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyActivity.class);
// Change is here...
serviceInten.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(serviceIntent);
}
}
In the manifest:
<receiver
android:name=".BootComplete"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter android:priority="500">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
And leave permission as it is.
I just received a new google-glass from a company which wants it to support their employees while picking and packing goods in their warehouse. For this reason they need a Server Client application which really isn't the problem.
I never did something with the Glass before and i want to know if it is possible to run a custom Application on boot and to jail the user into it.
Yesterday i rooted the device which gives me full access but i don't know how to go on.
Thank you!
Yes, it is possible.
As you have rooted the device, so you can create system app that can be recognised by the reboot event. Rest of the steps are totally similar to android mobile.
How to do it:
If you need to know the steps you can search on the web or you can try the following:
First, you need the permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also, in yourAndroidManifest.xml, define your service and listen for the BOOT_COMPLETED action:
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MySystemService.class);
context.startService(serviceIntent);
}
}
}
And now your service should be running when the phone starts up.
I added receiver in manifest
<receiver android:name=".PackageReceiver"
android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
And this is my BroadcastReceiver
public class PackageReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.e("noti", "sucess");
//Start Notification Service
Intent i = new Intent(context, NotificationService.class);
context.startService(i);
}}
..
When I install this package, It's didn't work...
This package don't have activity.(only service and resource)
Is this a problem?
Then..
How to call BroadcastReceiver in this package without activity?
This is a question that is asked a lot.
Check out this Commonsware blog.
Starting with 3.1 when applications are installed they are in a “stopped” state so they will not be able to run until the user explicitly launches them. Pressing Force Stop will return them to this state.
There needs to be an active component in your application that the user can start. When your activity gets started, your BroadcastReceiver will be registered.
You have just declared and implemented a BroadcastReceiver, but you haven't started it yet, you need an entry point to start your receiver ( activity or service )
here is the code to register
PackageReceiver packageReceiver= new PackageReceiver(this);
registerReceiver( packageReceiver, new IntentFilter("android.intent.action.PACKAGE_ADDED"));
I am trying to make an App in Titanium which launches on Startup i.e. as the mobile device startsup. I have seen code written at several places which states to do entry into the andsoid manifest file and some code like
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, YourActivity.class);
context.startActivity(myIntent);
}
But i am not able to figure out that where to put this code. In which file ?? and where ?
This 2 answers will do what you need:
Start BroadcastReceiver after some system broadcast:
https://stackoverflow.com/a/7877466/988434
Start BroadcastReceiver on boot:
https://stackoverflow.com/a/8544151/988434
in you BroadcastReceiver you'll implement just call whatever Service/Activty you need.
There an example for that in the question for the 2 answers above.
Tell me if you have any problems unanswered after reading those =].
You have to listen to the BOOT_COMPLETED intent filter. The piece of code you just quoted is from a BroadcastReceiver which will be firing up when the device will boot.
This class has to extend from BroadcastReceiver:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
...
}
}
Then, you have to register that receiver in your manifest file by doing the following:
<receiver
android:enabled="true"
android:name="your_package.BootReceiverClassName"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
Also you need the following permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
By the way you have to make sure that the app is not installed on the SD Card otherwise it won't work (but there are possible workarounds).
can any one tell me "how to lauch an 3rd application automatically when the android phone is switched on?". As I want to launch an application which is written by me when the device is turned on.
I will be waiting for any valuable reply .
Thanks in Advance,
you need an implementation of BroadcastReceiver, which is intended on BOOT_COMPLETED action. Like this:
public class OnStartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Runtime.getRuntime().exec("your command");
// but it is better here to do that:
Intent myIntent = new Intent(context, YourActivity.class);
context.startActivity(myIntent);
}
}
Also, you should add receiver tag to your manifest file with android:name = your fully qualified name of OnStartReceiver and intent-filter tag nested with BOOT_COMPLETED as intent name, like this:
<receiver android:name=".onStartReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver >