Sory everyone。首先,由于AUTO START MANAGER的原因,当我的设备上的应用程序关闭时,我的接收器不起作用。我感到很愚蠢......当我试图解决它时,我学到了很重要的东西。当应用关闭时,广播接收器和服务不工作(安卓)
首先在Android 6.0许可请求 Broadcast Receivers not working in Android 6.0 Marshmallow
其次:Android的 - 复制手机状态的棒棒糖
http://www.skoumal.net/en/android-duplicated-phone-state-broadcast-on-lollipop/
感谢广播...
我是新Android和我想学广播接收器&服务。此代码BroadcastReceiver监听摘机和空闲状态我的电话并发送意图服务做一些事情。而应用程序运行一切正常。关闭应用程序后,BroadcastReceiver和Service不起作用。
更新:另外我注意到,当应用程序运行和everythng正常,服务启动两次,并停止两次。
我看到这个消息,同时服务正在启动:
Toast.makeText(上下文 “” + phoneState +”“+记录,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),“service work”,Toast.LENGTH_SHORT).show();
再次1. Toast.makeText(上下文 “” + phoneState +”“+记录,Toast.LENGTH_SHORT).show();
2.再次Toast.makeText(getApplicationContext() “的服务工作”,Toast.LENGTH_SHORT).show()
我看到这个消息,同时服务回采:
吐司。 makeText(context,“”+ phoneState +“”+ record,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),“service will be stopped。”,Toast.LENGTH_SHORT).show();
再次1. Toast.makeText(上下文 “” + phoneState +”“+记录,Toast.LENGTH_SHORT).show();
again 2. Toast.makeText(getApplicationContext(),“service will be stopped。”,Toast.LENGTH_SHORT).show();
一切都重复。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="devapp.deneme">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:enabled="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>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />
<receiver
android:name="devapp.deneme.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
广播接收机
package devapp.deneme;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver(){
}
Intent service = null;
Bundle bundle = null;
String phoneState = null;
boolean record = false;
@Override
public void onReceive(Context context, Intent intent) {
service = new Intent(context, MyService.class);
bundle = intent.getExtras();
phoneState = bundle.getString(TelephonyManager.EXTRA_STATE);
if (phoneState!=null){
if ((phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))||(phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE))){
service.putExtra("durum", phoneState);
if (phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
record = true;
}
else {
record = false;
}
}
service.putExtra("record",record);
Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show();
context.startService(service);
}
}
}
服务
package devapp.deneme;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand (Intent intent, int flags, int startId){
boolean record = intent.getBooleanExtra("record", false);
if (record) {
Toast.makeText(getApplicationContext(), "service work", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "service will be stopped.", Toast.LENGTH_SHORT).show();
stopSelf();
}
return super.onStartCommand(intent, flags, startId);
}
}
通过关闭,如果你的意思强制关闭,那么它不会工作。 – Shaishav
使用任务管理器向右或向左滑动 – Murat
这是所有关于API级别和权限:http://stackoverflow.com/questions/33036523/broadcast-receivers-not-working-in-android-6-0-marshmallow – Murat