[英]is there a way to monitor application in background & invisible from user
我要做的是创建一个无需用户干预即可执行其功能的应用程序。 设备的“应用程序”页面上不应包含任何appicon。 安装后,用户无需知道设备中正在运行的应用程序。 我尝试在演示应用程序中使用“没有启动器活动”,但是它没有运行应用程序的代码,这很明显。 有没有办法完成这项任务,这有意义吗?
是的,这是可能的,而且很有意义。 但是,例如,这需要做很多事情。
1)。 您需要将应用程序设置为启动启动方式,这意味着只要用户重新启动移动设备或设备,您的应用程序就会自动启动。
<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)。 显然,你必须让应用程序没有启动模式,因为它的第一个活动,然后调用次活动不作为活动相关的服务。
因此,基本上,您必须创建类似这样的内容。
public class AppService extends WakefulIntentService{
// your stuff goes here
}
在从mainActivity调用服务时,应按以下方式定义它。
Intent intent = new Intent(MainActivity.this, AppService.class);
startService(intent);
hideApp(getApplicationContext().getPackageName());
hideApp //用它在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)。 然后在清单中定义此服务,如下所示。
<service android:name=".AppService" >
</service>
编辑
WakefulIntentService
是一个新的抽象类。 请在下面检查。 因此,创建一个新的Java文件并将beloe代码粘贴到其中。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.