Android隐藏图标自启动服务

    xiaoxiao2021-04-12  27

    上面说要做一个功能,说应用要做成服务型的,没有界面,没有图标,而且开机自启动。对于我这种菜鸟一听就蒙圈了,捯饬了一周也没什么进展,估计当时的方向不对,哈哈。。。不管怎么样,隔了半个月,又做出来了。今天天气真好啊!废话不多说,看代码,biubiubiu……….

    1.一个activity、一个BroadcastReceiver、一个service.

    2.在Manifest上添加几行代码。 一盆不显示应用图标、自启动服务的小应用就做好了。

    代码块

    MainActivity

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.startService(new Intent(this, TestService.class)); }

    Service

    public void onCreate() { super.onCreate(); Toast.makeText(getApplicationContext(), "xhm", Toast.LENGTH_LONG).show(); } @Override public IBinder onBind(Intent intent) { Toast.makeText(getApplicationContext(), "不xhm", Toast.LENGTH_LONG).show(); return null; }

    Service

    @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub Intent mBootIntent = new Intent(arg0, TestService.class); arg0.startService(mBootIntent); }

    最后不要忘了配置Manifest.xml

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <service android:name=".TestService" android:label="@string/app_name"> </service> <receiver android:name=".BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <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" /> <data android:host=".MainActivity" android:scheme="com.xhm.hideiconxhm"/> </intent-filter> </activity>

    源码下载

    转载请注明原文地址: https://ju.6miu.com/read-668276.html

    最新回复(0)