亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
android 不打開APP的情況下監(jiān)聽耳機(jī)插拔情況
漂亮男人
漂亮男人 2017-06-24 09:42:57
[Android討論組]

現(xiàn)在的需求是。開機(jī)之后或者不打開App的情況下監(jiān)聽耳機(jī)的插拔情況。從而執(zhí)行不同的操作。
現(xiàn)在的思路是。
開機(jī)啟動一個service,在service中監(jiān)聽耳機(jī)插拔情況。如果耳機(jī)插入則打開一個Activity。否則關(guān)閉當(dāng)前Activity。
整個思路是否有問題??

漂亮男人
漂亮男人

全部回復(fù)(2)
扔個三星炸死你

問題就是service進(jìn)程如何存活

天蓬老師

import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import java.util.List;

public class HeadPhoneService extends Service {

public HeadPhoneService() {
}
@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    super.onCreate();
    Log.e("xxxx","service start");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(headsetReceiver, intentFilter);
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    Intent service = new Intent(this, HeadPhoneService.class);
    this.startService(service);
    super.onDestroy();
}

private BroadcastReceiver headsetReceiver=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
            Log.e("xxxx","headsetchange");
            if (intent.hasExtra("state")) {
                int state = intent.getIntExtra("state", 0);
                if (state == 1) {

                } else if(state == 0){

                }
                Log.e("xxxx","headphone"+state);
            }
        }
    }
};

}

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號