Android通过action启动服务,android-启动服务

下面是一个自动启动应用程序的完整示例

AndroidManifest文件<?xml  version="1.0" encoding="utf-8"?>

package="pack.saltriver" android:versionCode="1" android:versionName="1.0">

autostart.javapublic 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-每次在执行应用程序之后启动设备时,都会弹出。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();

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Framework 中查看启动服务耗时,可以通过 adb shell dumpsys activity services 命令来获取。这个命令会输出所有正在运行的服务的信息,包括它们的启动时间和耗时。 例如,假设我们要查看系统中当前正在运行的所有服务启动耗时,可以在终端中输入以下命令: ``` adb shell dumpsys activity services ``` 输出结果中,每个服务的信息都会以如下形式显示: ``` SERVICE com.example.MyService * ServiceRecord{123456 u0 com.example/.MyService} intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example/.MyService} packageName=com.example processName=com.example baseDir=/data/app/com.example-1/base.apk dataDir=/data/data/com.example app=ProcessRecord{7890 u0 com.example:myprocess/xyz} createTime=-2m28s824ms startingBgTimeout=-- lastActivity=-2m28s824ms restartTime=-2m28s824ms createdFromFg=true startRequested=true stopIfKilled=true callStart=true lastStartId=0 bindings.size=0 executingStart=-1 startId=-1 delayedStop=false stopWithTask=false lastStartForeground=false noisy=false fgg=false explicitIdle=false comp=null connection=null delayed=true delayedStop=false stopWithTask=false lastStartId=-1 destroyTime=-1 crashCount=0 restartDelay=0 restartCount=0 nextRestartTime=-1 proc=start executeNesting=1 callingIdentity=System whitelistManager=null mBoundApplication=AppBindData{...} mServiceInfo=ServiceInfo{...} mPackageName=com.example grantedUriPermissions=0 pendingStarts.size=0 lastPrincipalResuming=null retainInstance=false mStartCompatibility=0 mDispatchedStarts=[StartInfo{...}] mDelayedStop=-- mDestroyed=false mStopIfKilled=false mStopWithTask=false mStartRequested=true mDelayed=true mLastStartId=-1 mExecuteNesting=1 mAllowCancel=true mResultTo=null mService=null mHasAutoCreateConnections=false mCreateBytes=-1 mRestored=false mDestroyedByApp=false mIsForeground=false mForegroundId=0 mForegroundNoti=null mCrashCount=0 mRestartCount=0 ``` 其中,createTime 表示服务创建时间,lastActivity 表示最后一次活动时间,restartTime 表示服务重新启动时间,executeNesting 表示执行嵌套深度(如果超过 1,则表示服务正在执行其他操作,如绑定等),mCreateBytes 表示创建服务时使用的内存大小,mCrashCount 表示服务崩溃次数,mRestartCount 表示服务重启次数等。 注意,dumpsys activity services 命令会输出大量信息,如果要查找特定服务启动时间,可以使用 grep 命令进行过滤。例如,要查看 com.example.MyService启动时间,可以使用以下命令: ``` adb shell dumpsys activity services | grep com.example.MyService ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值