栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android

好吧,这是一个自动启动应用程序的完整示例

AndroidManifest文件

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="pack.saltriver" android:versionCode="1" android:versionName="1.0">    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <receiver android:name=".autostart"> <intent-filter>     <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter>        </receiver>        <activity android:name=".hello"></activity>        <service android:enabled="true" android:name=".service" />    </application></manifest>

自动启动

public class autostart extends BroadcastReceiver {    public void onReceive(Context context, Intent arg1)     {        Intent intent = new Intent(context,service.class);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent);        } else { context.startService(intent);        }        Log.i("Autostart", "started");    }}service.javapublic class service extends Service{    private static final String TAG = "MyService";    @Override    public IBinder onBind(Intent intent) {        return null;    }    public void onDestroy() {        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();        Log.d(TAG, "onDestroy");    }    @Override    public void onStart(Intent intent, int startid)    {        Intent intents = new Intent(getbaseContext(),hello.class);        intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        startActivity(intents);        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();        Log.d(TAG, "onStart");    }}

hello.java-执行一次Applicaton后,每次启动设备时都会弹出此窗口。

public class hello extends Activity {       public void onCreate(Bundle savedInstanceState)     {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Toast.makeText(getbaseContext(), "Hello........", Toast.LENGTH_LONG).show();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/364250.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号