问题
What I want to do is to create an application which can perform it's feature without user interaction. This shouldn't have any appicon at Applications page in Device. After installation user don't need to aware of application running in device. I tried with No Launcher Activity in a Demo Application but it is not running code of application and that's obvious. Is there a way to accomplish this task, Does this make any sense?
回答1:
Yes it is possible, and it makes lot of sense. But it takes lot's stuff to do, for example.
1). You need to make your app as boot start-up means whenever user restart mobile or device your app should automatically start's.
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".OnBootReceiver" >
<intent-filter
android:enabled="true"
android:exported="false" >
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name=".OnGPSReceiver" >
</receiver>
2). Obviously you have to make app with no launcher mode as it's first activity and then call second activity as a service not as an activity.
so basically you have to create something like this.
public class AppService extends WakefulIntentService{
// your stuff goes here
}
and while calling service from your mainActivity define it like this.
Intent intent = new Intent(MainActivity.this, AppService.class);
startService(intent);
hideApp(getApplicationContext().getPackageName());
hideApp // use it outside the mainActivity.
private void hideApp(String appPackage) {
ComponentName componentName = new ComponentName(appPackage, appPackage
+ ".MainActivity");
getPackageManager().setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
3). Then in manifest define this service as like below.
<service android:name=".AppService" >
</service>
Edit
WakefulIntentService
is a new abstract class. Please check below. So create a new java file and paste the beloe code in it.
abstract public class WakefulIntentService extends IntentService {
abstract void doWakefulWork(Intent intent);
public static final String LOCK_NAME_STATIC = "test.AppService.Static";
private static PowerManager.WakeLock lockStatic = null;
public static void acquireStaticLock(Context context) {
getLock(context).acquire();
}
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
LOCK_NAME_STATIC);
lockStatic.setReferenceCounted(true);
}
return (lockStatic);
}
public WakefulIntentService(String name) {
super(name);
}
@Override
final protected void onHandleIntent(Intent intent) {
doWakefulWork(intent);
//getLock(this).release();
}
}
来源:https://stackoverflow.com/questions/22168858/is-there-a-way-to-monitor-application-in-background-invisible-from-user