2016-07-25 65 views
0

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正常,服務啓動兩次,並停止兩次。

我看到這個消息,同時服務正在啓動:

  1. Toast.makeText(上下文 「」 + phoneState +」「+記錄,Toast.LENGTH_SHORT).show();

  2. 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()

我看到這個消息,同時服務回採:

  1. 吐司。 makeText(context,「」+ phoneState +「」+ record,Toast.LENGTH_SHORT).show();

  2. 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); 
    } 
} 
+0

通過關閉,如果你的意思強制關閉,那麼它不會工作。 – Shaishav

+0

使用任務管理器向右或向左滑動 – Murat

+0

這是所有關於API級別和權限:http://stackoverflow.com/questions/33036523/broadcast-receivers-not-working-in-android-6-0-marshmallow – Murat

回答

0

啓動服務爲粘性的。更改服務的onStartCommand方法類似像下面

公衆詮釋onStartCommand(意向意圖,詮釋標誌詮釋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 START_STICKY; // or START_REDELIVER_INTENT or START_STICKY_COMPATIBILITY 
} 
+0

Sory nothing更改。我之前嘗試過,但謝謝。 – Murat

+0

我編輯了我的答案,請檢查此。 –

+0

我已經從2天內消除了它。我之前嘗試過。在我看來,接收器有問題。但我無法解決。另外我嘗試了不同的兩個設備。 – Murat

0

您必須添加在服務類中的下列

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

     // your business logic 

    return START_STICKY;