2016-08-11 1 views
0

전화를받은 후 토스트를 표시하려고하는데 방송 수신기를 등록하는 데 필요한 모든 것을 구현했지만 토스트를 표시하지 않습니다. 내가 Hello World 말을 한 MainActivity 하나의 기본 레이블이통화를위한 Android 브로드 캐스트 수신기가 작동하지 않습니까? (마시 멜로우)

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.suhas.msgmanager"> 

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/msgis" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 


    <activity android:name="com.example.suhas.msgmanager.MyDialog" android:launchMode="singleTask" 
     android:theme="@android:style/Theme.Translucent" /> 

    <service android:name="com.example.suhas.msgmanager.ChatHeadService"></service> 

    <receiver android:name=".MyCallReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
     </intent-filter> 
    </receiver> 

    <activity android:name=".AddMessageActivity"> 
    </activity> 
</application> 

</manifest> 

-

package com.suhas.callreceiver; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 

public class MyCallReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
     // This code will execute when the phone has an incoming call 

     // get the phone number 
     String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 
     Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show(); 
     Log.d("MyTrack call", "call receive"); 

    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
      TelephonyManager.EXTRA_STATE_IDLE)) 
    { 
     Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show(); 
    } 
    else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
      TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
     // This code will execute when the call is disconnected 

    } 
} 
} 

의 AndroidManifest.xml - 나는 마시 멜로 장치에

MyCallReceiver.java을이 프로그램을 실행하려합니다.

+0

여기 보라 : http://www.tutorialforandroid.com/2009/01/get-phone-state-when-someone-is-calling_22.html – Shaishav

+0

이 패키지의 이름을 확인 했 . 응용 프로그램 패키지 이름은 com.example.suhas.msgmanager이고 com.suhas.callreceiver 패키지 – Sandeep

+0

에서 수신기를 정의했습니다. marshmallow 장치에서 실행 중이므로 marshmallow에서 런타임 권한을 어떻게 지정할 수 있습니까? 원인 marshmallow는 런타임 권한이 필요합니다 –

답변

0

수신자에 잘못된 패키지 이름을 지정했습니다.

아래로 수신기를 정의해야합니다 :

<receiver android:name="com.suhas.callreceiver.MyCallReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
     </intent-filter> 
    </receiver> 
+0

난 그냥 적절한 패키지 이름과 그 잘 작동하여 코드를 테스트했습니다. – Sandeep

+0

marshmallow 장치에서 실행 중이므로 marshmallow에서 런타임 권한을 어떻게 지정할 수 있습니까? 원인 marshmallow는 런타임 권한이 필요합니다 –

+0

런타임 권한에 대한 링크를 확인하십시오. https://developer.android.com/training/permissions/requesting.html – Sandeep

7

마시 멜로 버전의 경우, 우리는 허가를 작동하기 위해 활동 안에 만들 수있는 개념이라고 런타임 권한이 있습니다. 런타임 권한은 처음 활동을 실행하는 동안 런타임에 특정 권한을 사용자에게 묻는 방법을 제공합니다.

이 사용자가 지정해야 할 두 가지 있습니다 :

//

public final static int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 11; 

허가를 어떤 일정한 수를 지정 // 한 OnCreate 방법 코드의 비트 다음 지정

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Here, thisActivity is the current activity 
    if (ContextCompat.checkSelfPermission(getApplicationContext(), 
      Manifest.permission.READ_PHONE_STATE) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.READ_CONTACTS)) { 
     } else { 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.READ_PHONE_STATE}, 
        MY_PERMISSIONS_REQUEST_READ_PHONE_STATE); 
     } 
    } 
} 

// 런타임에 권한을 묻는 팝업 창을 표시하는이 메서드를 지정하십시오.

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       } else { 
      } 
      return; 
     } 
    } 
} 

이 >> 응용 프로그램 앱 >> 장치 설정에서 시간의 허가 또는 수동을 실행할 필요가 마시 멜로에 따라 >> 산들 장치

+0

감사합니다. 완벽하게 작동했습니다. –

+0

저를 위해 일했습니다 –

+0

적어도 3 시간 동안 검색 한 후 나는 마침내 올바른 대답을 발견했습니다. – Matthew556

0
대상 API (23)에

이상 작동 할 수있는 방법을 제공하여 응용 프로그램을 선택합니다 허가

this link can help you

관련 문제