2017-12-21 1 views
1

하나는 내게 안드로이드 개발에 새로운 도움을 청합니다. 부팅하는 동안 브로드 캐스트 레코더에 전화하고 싶습니다. 그러나 그것은 나의 소스 코드를 내가 매니페스트부팅하는 동안 BroadcastReceiver를 호출하는 방법?

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.tajmirkhan.broadcasetreciver_new"> 

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

    <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> 

     <receiver android:name=".Bootservice" android:exported="true" android:enabled="true"> 
      <intent-filter android:priority="500"> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      </intent-filter> 

     </receiver> 
    </application> 

</manifest> 

의 권한을 부여

working.here이며,이 사람은 내 브로드 캐스트 리시버 클래스

public class Bootservice extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.e("dd","above the onReceive mthode"); 
     if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ 
      Toast.makeText(context, "boot completed ", Toast.LENGTH_SHORT).show(); 
      Log.e("dd","inisde the onReceive mthode"); 
     } 
    } 
} 

이가 내 mainActivity

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Log.e("dd","oncreat "); 

    } 
} 

답변

0

당신은 당신의 수신기에 줄을 추가 <category android:name="android.intent.category.HOME"/>를 호출하는 것을 잊지

<receiver 
     android:name=".Bootservice" android:exported="true" android:enabled="true"> 
     <intent-filter android:priority="500"> 
     <category android:name="android.intent.category.HOME"/> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 

    </receiver> 

이 라인 :

<category android:name="android.intent.category.HOME"/> 

당신이 홈 버튼을 누르면 앱이에 옵션으로 표시됩니다 것을 나타냅니다 실행기 홈 또는 집 활동을 실행하십시오 (활동에 대한 매니페스트에이 범주가있는 모든 응용 프로그램과 함께). 좀 더 간단하게하기 위해, 홈 버튼을 누를 때마다, CATEGORY.HOME 범주와 ActionMan을 가진 AndroidManifest.xml의 인텐 트 필터에있는 모든 애플리케이션이 나열됩니다 (일부 애플리케이션을 기본값으로 선택하지 않은 경우). 사용자가 시작할 홈을 선택하기위한 선택기.

해피 코딩 !!

+0

내가이 코드 조각을 적용하지만이 작동하지 않았다. –

0

이이 수정 있는지 알고 있지만 수정하지 마십시오 :

if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) 

에 :

if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) 
관련 문제