2017-12-21 16 views
1

私はアンドロイド開発の新機能です。私は起動時にその放送レコーダーに電話したい。しかし、その私のソースコード起動時にBroadcastReceiverを呼び出す方法は?

がworking.hereではない私は、マニフェスト

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

の許可を与えられ、この1つは

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"); 
     } 
    } 
} 

私broadcastreceiverクラスで、これは私です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"/> 

あなたはホームボタンを押すと、アプリがへのオプションとして表示されることを示しますランチャーのホームまたは自宅のアクティビティ(アクティビティのマニフェストにこのカテゴリを持つすべてのアプリケーションとともに)を起動します。もっと簡単にするため、ホームボタンを押すたびに、AndroidManifest.xml内のCATEGORY.HOMEカテゴリとIntent-FilterのAction_Mainを持つ携帯電話にインストールされているすべてのアプリケーションが一覧表示されます(デフォルトではアプリケーションを選択していない限り)。ユーザーがどのホームを起動するかを選択するためのチューザー。

ハッピーコーディング!

+0

iは、コードのこの部分を適用するが、それはうまくいきませんでした。 –

0

これが修正される場合は知っているが、変更しようとしないでください:

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

へ:

if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) 
関連する問題