How I can install apk automatically when the sdcard put on? - android

I have not any idea. How can I install apk automatically when the sdcard put on?
However, I have got an problem, When I register the receiver that listened to the ACTION_MEDIA_SHARED in AndroidManifest.xml Em... I create a Receiver that extends BroadcastReceive, I override OnReceive(). But, finally, the Receiver can not get any Action. Here is my code. Frustrating!!!!!
<receiver android:name=".SdcardPutOnListener"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_MEDIA_BAD_REMOVAL" />
<action android:name="android.intent.action.ACTION_MEDIA_MEDIA_CHECKING" />
<action android:name="android.intent.action.ACTION_MEDIA_EJECT" />
<action android:name="android.intent.action.ACTION_MEDIA_MOUNTED" />
<action android:name="android.intent.action.ACTION_MEDIA_NOFS" />
<action android:name="android.intent.action.ACTION_MEDIA_REMOVED" />
<action android:name="android.intent.action.ACTION_MEDIA_SHARED" />
<action android:name="android.intent.action.ACTION_MEDIA_UNMOUNTABLE" />
<data android:scheme="file" />
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
public class SdcardPutOnListener extends BroadcastReceiver {
final static String TAG = "SdcardPutOnListener";
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "receive broadcast " + intent.getAction());
}
}

You could create an app that watches for the ACTION_MEDIA_MOUNTED broadcast, then looks at external storage in a well-known spot for an APK file, then calls startActivity() with an ACTION_VIEW Intent on the path to that APK file, with the right MIME type (application/vnd.android.package-archive).
If you are expecting this to be built into the operating system, it is not.

Related

How to detect that new app has been installed on the device?

How does CleanMaster app detect that a new app has been installed on the device? Whenever I install a new app, I get a popup asking if I want to move the app to SD card.
I am trying to code similar behaviour but cannot find a way to do it.
There is the ACTION_PACKAGE_ADDED Broadcast Intent, but the application being installed doesn't receive this.
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Android provides String android.content.Intent.ACTION_PACKAGE_ADDED ="android.intent.action.PACKAGE_ADDED" Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast.
You can write a BroadcastReceiver receiving the Intent.ACTION_PACKAGE_ADDED for that.
For this You need to write a receiver class like this
public class AppInstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
//Perform Your opeartion
}
}
And register it in manifest like.
<receiver android:name="com.example.AppInstallReceiver" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<data android:scheme="package" />
</intent-filter>
</receiver>

Cannot receive Uninstall broadcast

I am trying to detect the uninstall action of my application. Till now, I have got a specific code that catch the uninstall action and inflate an Activity. Here is the code:
Manifest:
<activity
android:name=".UninstallActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</activity>
I have created a simple Activity called UninstallActivity and It works fine. When the user try to uninstall the app this Activity has been inflated.
I am trying to listen on those intents with a Receiver instead of Activity but I have failed to get this action. My code is:
Manifest:
<receiver android:name=".PackageUninstallReceiver" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</receiver>
PackageUninstallReceiver:
public class PackageUninstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("uTag", "In the PackageUninstallReceiver onReceive()");
if (intent.getAction().equals(Intent.ACTION_DELETE) && intent.getDataString().contains(context.getPackageName())) {
Log.d("uTag", "Uninstallation is being happened....");
}
}
}
First, is it possible to listen to this Intent with the receiver?
If yes, what is wrong with my code?
The Intent used to start an Activity (in this case, an Intent to VIEW or DELETE a PACKAGE) is a completely different thing from a braodcast Intent. They share some of the same properties, but are still completely different things. a broadcast Intent will never start an Activity and an Intent used to start an Activity will never be seen by a BroadcastReceiver.
Therefore, the answer to your question
First, is it possible to listen to this Intent with the receiver?
is "no".
The actions you are listening are generic and could be applied in any context with a different schema. What you should be listening to is the package changing.
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>

How to create a Receiver for creating a new Event

I have created an application that successively adds appointments to a calendar. However i wish now to be able to click the "New Event" button in a calendar program eg. aCalendar and have my program popup. and i think i am a little lost.
in my AndroidManifest.xml
<receiver
android:name="com.johnny.CalendarAdd"
android:enabled="true"
>
<intent-filter>
<data android:pathPrefix="vnd.android.cursor.item/event" />
<action android:name="android.intent.action.EDIT" />
</intent-filter>
</receiver>
tryed to change it to.
<activity android:name="CalendarAdd">
<intent-filter>
<data android:pathPrefix="vnd.android.cursor.item/event" />
<action android:name="android.intent.action.EDIT" />
</intent-filter>
</activity>
In class file
package com.johnny;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class CalendarTest extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("CalendarAdd", "CalendarAdd.onReceive called!");
}
}
when i click the "New Event" button i don't get my app in the list.
I have had a look-around and think i have missed something simple or i am Totally on the wrong path.
Thanks for any Help in advance.
You can't achieve it using Broadcast Receiver, Broadcast intent-filter will work only when some app broadcast that particular intent, e.g connectivity state got changed, screen got turned off, battery low etc are correct example of system broadcasts. While create calendar event can't be a broadcast & thats why you can't receive it.
From android documentation regarding Broadcast Receivers:
A broadcast receiver is a component that responds to system-wide
broadcast announcements. Many broadcasts originate from the system—for
example, a broadcast announcing that the screen has turned off, the
battery is low, or a picture was captured. Applications can also
initiate broadcasts—for example, to let other applications know that
some data has been downloaded to the device and is available for them
to use. Although broadcast receivers don't display a user interface,
they may create a status bar notification to alert the user when a
broadcast event occurs. More commonly, though, a broadcast receiver is
just a "gateway" to other components and is intended to do a very
minimal amount of work. For instance, it might initiate a service to
perform some work based on the event.
For achieving desired functionality, you need to register intent-filter for your activity.
Also your manifest should contain read/write calendar permission and event edit/insert intent-filter, e.g.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.CalanderTest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name="MyActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.INSERT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/event" />
</intent-filter>
</activity>
</application>
</manifest>
for testing it out do below in onclick of some test button:-
#Override
public void onClick(View view) {
super.onClick(view);
if (view.getId()==R.id.create_event)
{
Intent i = new Intent(Intent.ACTION_INSERT);
i.setType("vnd.android.cursor.item/event");
startActivity(i);
}
}
On clicking of my test button i got menu like in below image, so above code works fine to show your app in App chooser dialog for adding/editing calendar events.
Now in your activity you can handle this intent using getIntent() and perform action accordingly.
You can have a look here for extras supported by Calendar event.
Like Events.TITLE,Events.EVENT_LOCATION etc. If you are going to create your own calendar event creator, then you need to handle all such extras Gracefully, for awesome user experience.
Place the <intent-filter> you defined inside your <activity> instead. The user should be given a list of apps (including yours) that can handle that action, so they can select yours if they want to.
<receiver>s are for receiving BroadcastIntents, which are a bit different.
Use <receiver> as you originally did, and android:name should be com.johnny.CalendarTest if you wish to register your receiver, since your receiver class name is CalendarTest.
See here
android:name
Required name of the class implementing the receiver, deriving from BroadcastReceiver.
However i wish now to be able to click the "New Event" button in a
calendar program eg. aCalendar and have my program popup.
Have you tried the various action and category filter declarations in your <intent-filter>.
For instance:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.OPENABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/myapp" />
</intent-filter>
See if any of those help you.
See also: http://developer.android.com/reference/android/content/Intent.html
From CalendarController, I can almost be sure that these intent are explicit intent with specific component set. For example, in launchEditEvent method.
private void launchEditEvent(long eventId, long startMillis, long endMillis, boolean edit) {
Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId);
Intent intent = new Intent(Intent.ACTION_EDIT, uri);
intent.putExtra(EXTRA_EVENT_BEGIN_TIME, startMillis);
intent.putExtra(EXTRA_EVENT_END_TIME, endMillis);
intent.setClass(mContext, EditEventActivity.class);
intent.putExtra(EVENT_EDIT_ON_LAUNCH, edit);
mEventId = eventId;
mContext.startActivity(intent);
}
The intent is set to be handled by EditEventActivity. So it is impossible to receive such intents in your application. I think the Calendar App make it as an explicit intent because it regard this as an in-app event. If this is not an explicit intent, your code should work, because the component in Calendar App is almost same with what you did.
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.INSERT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/event" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.INSERT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/event" />
</intent-filter>
The aCalendar New Event Intent is as this.
{act=android.intent.action.EDIT typ=vnd.android.cursor.item/event flg=0x20000000 cmp=org.withouthat.acalendar/.edit.EditActivity (has extras)}
And Google Calendar App New Event Intent is as this.
{act=android.intent.action.VIEW cmp=com.android.calendar/.event.EditEventActivity
They are explicit intent.
Your can not receive this intent in your app.
More about intent click here
<activity android:name="com.johny.CalendarTest"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<data android:host="com.android.calendar" />
<data android:host="calendar" />
<data android:scheme="content" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<data android:mimeType="vnd.android.cursor.item/event" />
</intent-filter>
</activity>
This was the exact code i used and got to work.
Most of The Answers helped in getting the response.
The reward went to the person who put most effort into it and achieved what was a result.
The old answers show me the direction, but I had to do a small modification to make it work:
<activity
android:name=".YourActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.INSERT" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/event" />
</intent-filter>
</activity>

Android: receiving intent sent by system ACTION_PACKAGE_RESTARTED

I am new to android. I get completely stuck in using ACTION_PACKAGE_RESTARTED in my application
I have removed pacakge from my emulator, also added using adb install but get nothing. Start an app. close that one and again start that app. nothing seems work for me. There is no log in logcat.
Is there anything that i'm missing? Please help
public class RestartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action= intent.getAction();
Log.i("D", "Inside receiver");
}
And here is the manifest file
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".ReceiverTest">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.ACTION_PACKAGE_RESTARTED" />
</intent-filter>
</receiver>
</application>
the value specified in the intent filter is incorrect..actual value is
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
and this broadcast can be received for other packages only. Restarted application/package doesn't receive this broadcast.
You should add a data specification to the intent-filter:
<data android:scheme="package" />

Android Notification App

I'm currently working on an android application. I have to log any new installed app name whenever the user is installing/downloading a new third party app. How can I get the notification if the user is installing a new app. Thanks in advance.
Java File
public class ApplicationBroadcastService extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
System.out.print("-------");
}
}
Manifest
<receiver android:name=".applicationlog.ApplicationBroadcastService">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
</intent-filter>
</receiver>
But still I do not enter the onReceive method, when I am installing/uninstalling any app.
Here is the solution:
I did a small change in my Manifest file.
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
Now it's working fine.. :)
Thanks again #willytate
Ajay,
You will need to setup a BroadcastReceiver with an intent filter to receive the following Action: ACTION_PACKAGE_ADDED then from the onReceive() method of the BroadcastReceiver you can launch a Notification.
Take a look at the intent documentation. You are looking for ACTION_PACKAGE_INSTALL (which seems to be never used, see comments) and ACTION_PACKAGE_REMOVED.
You can listen for the android.intent.action.PACKAGE_ADDED intent.

Categories

Resources