av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java - Android Studio 中的Service問(wèn)題

瀏覽:111日期:2024-02-23 11:13:33

問(wèn)題描述

新手,在學(xué)習(xí)Service一開(kāi)始就碰到了問(wèn)題是根據(jù)教程做的,在開(kāi)啟服務(wù)時(shí)閃退MyService.java

package com.example.administrator.myhhhhh;

import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;

public class MyService extends Service {

public MyService() {}@Overridepublic void onCreate() { Log.i('LOG','onCreat'); super.onCreate();}@Overridepublic IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException('Not yet implemented');}

}

MainActivity.java

package com.example.administrator.myhhhhh;

import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private Button start,stop;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button start=(Button)findViewById(R.id.button); Button stop=(Button)findViewById(R.id.button2); start.setOnClickListener(this); stop.setOnClickListener(this);}@Overridepublic void onClick(View v) { Intent intent=new Intent('MyService'); switch (v.getId()){case R.id.button: startService(intent); break;case R.id.button2: stopService(intent); break; }}

}

Manifest.xml<?xml version='1.0' encoding='utf-8'?><manifest xmlns:android='http://schemas.android.com/apk/res/android'

package='com.example.administrator.myhhhhh'><application android:allowBackup='true' android:icon='@mipmap/ic_launcher' android:label='@string/app_name' android:roundIcon='@mipmap/ic_launcher_round' android:supportsRtl='true' android:theme='@style/AppTheme'> <activity android:name='.MainActivity'><intent-filter> <action android:name='android.intent.action.MAIN' /> <category android:name='android.intent.category.LAUNCHER' /></intent-filter> </activity> <serviceandroid:name='.MyService'><intent-filter> <action android:name='MyService'/></intent-filter> </service></application>

</manifest>

Logcat 錯(cuò)誤提示:05-04 21:28:44.377 21214-21214/com.example.administrator.myhhhhh E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.administrator.myhhhhh, PID: 21214 java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=MyService } at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1851) at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1880) at android.app.ContextImpl.startService(ContextImpl.java:1864) at android.content.ContextWrapper.startService(ContextWrapper.java:516) at com.example.administrator.myhhhhh.MainActivity.onClick(MainActivity.java:26) at android.view.View.performClick(View.java:4918) at android.view.View$PerformClick.run(View.java:20399) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5871) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1119) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)

05-04 21:28:44.434 21214-21214/com.example.administrator.myhhhhh I/Process: Sending signal. PID: 21214 SIG: 9

問(wèn)題解答

回答1:

Intent intent=new Intent('MyService');什么鬼?這個(gè)不管什么版本的android系統(tǒng)都無(wú)法調(diào)起來(lái)的,service啟動(dòng)的方式分文兩種,顯示調(diào)用和隱式調(diào)用:

1)顯示啟動(dòng):

Intent intent=new Intent(MainActivity.this,MyService.class);

通過(guò)class調(diào)起service,

2)隱式啟動(dòng):

Intent intent=new Intent('com.example.administrator.myhhhhh.MyService');

里面是service類(lèi)的絕對(duì)路徑(帶有包名部分),需要注意的是隱式啟動(dòng)在android最新版本(5.0以上)已經(jīng)被棄用(由于安全問(wèn)題),你需要對(duì)他進(jìn)行一些額外的處理(通過(guò)action獲取它的ComponentName屬性),以下提供你一個(gè)方式,代碼如下:

protected static synchronized void startService(Context context,String action){LogUtils.debug('startHeartBeatService.');if (context==null){ throw new NullPointerException('context must not be null.');}//add Urgent listener service.Intent mIntent=new Intent();mIntent.setAction(action);PackageManager pm = context.getPackageManager();List<ResolveInfo> resolveInfo = pm.queryIntentServices(mIntent, 0);// Make sure only one match was foundif (resolveInfo == null || resolveInfo.size() != 1) { LogUtils.debug('not found action like this ’com.hll.push.heartbeat’'); return;}// Get component info and create ComponentNameResolveInfo serviceInfo = resolveInfo.get(0);String packageName = serviceInfo.serviceInfo.packageName;String className = serviceInfo.serviceInfo.name;ComponentName component = new ComponentName(packageName, className);// Create a new intent. Use the old one for extras and such reuseIntent explicitIntent = new Intent(mIntent);// Set the component to be explicitexplicitIntent.setComponent(component);context.startService(explicitIntent); }

祝你好運(yùn)~

回答2:

Intent intent=new Intent(this,MyService.class);回答3:

自己的intent寫(xiě)錯(cuò)了

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 日本美女性生活 | 一本一道久久a久久精品蜜桃 | 香蕉视频一区二区三区 | 国产日韩一区二区 | 日本少妇视频 | 国产麻豆视频 | 亚洲一区二区在线播放 | 天天操夜夜操 | 国产精品毛片一区二区在线看 | 中文字幕专区 | 看黄色大片 | 人与拘一级a毛片 | 国产一区在线播放 | 成人免费激情视频 | 黄色大片在线 | 亚洲综合在线播放 | 欧美精品影院 | 精品国产99久久久久久宅男i | 中文字幕第一区综合 | 中文有码在线 | 欧美福利影院 | 在线播放国产精品 | 日本视频免费 | 日韩a在线观看 | 国产精品入口夜色视频大尺度 | 国产精品久久久久久久久久 | 欧美1级片| 精品视频免费在线观看 | 欧美 日韩 国产 成人 在线 | 日韩在线欧美 | 精品成人av | 国产区在线 | 午夜精品一区二区三区在线播放 | 性爱视频日本 | 亚久久 | 亚洲精品自拍视频 | 黄色一级毛片 | 亚洲成人免费网站 | av手机在线观看 | 黄色片免费观看 | 日韩中文字幕在线播放 |