2016-07-04 16 views
1

ブロードキャスト受信者を使用して、電話がオンになったときにいつでも自分のアプリを自動起動します。しかし私の場合はうまくいかない。私はプログラムの詳細を添付しています。誰かが私を助けてください、どこに遅れていますか?事前に感謝電話機の再起動時に受信者が機能しない

this is manifest file 

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     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=".reciever.BootStartReciever"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

     <service 
      android:name=".FetchSimNumber" 
      android:enabled="true" 
      android:exported="true"></service> 
    </application> 

</manifest> 

this is my broadcast reciever 

    public class BootStartReciever extends BroadcastReceiver { 
    public BootStartReciever() { 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
      Intent pushIntent = new Intent(context, FetchSimNumber.class); 
      context.startService(pushIntent); 

     } 
    } 
} 


this is my service class 

public class FetchSimNumber extends Service { 
    public FetchSimNumber() { 

      Intent intent = new Intent(FetchSimNumber.this,MainActivity.class); 
     startActivity(intent); 


    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO: Return the communication channel to the service. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 
} 

and this is my main activity 

public class MainActivity extends AppCompatActivity { 

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

     TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     String getSimSerialNumber = telemamanger.getSimSerialNumber(); 
     String getSimNumber = telemamanger.getLine1Number(); 
     ((TextView) findViewById(R.id.tv1)).setText(getSimSerialNumber); 
     ((TextView) findViewById(R.id.tv2)).setText(getSimNumber); 


    } 
} 
+1

あなたの受信機に= trueを有効に置きます。 –

+0

hello sagar、マニフェストファイル –

+0

はいを​​意味します。あなたは私の答えを見ることができます。 –

答えて

1
public class BootStartReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
      /* Setting the service here */ 
     Intent i = new Intent(context, Yourservice.class) 
     context.startService(i); 
} 

とマニフェストファイル内:

<receiver android:name=".BootReceiver" 
     android:enable="true"> 
         <intent-filter> 
           <action android:name="android.intent.action.BOOT_COMPLETED" /> 
           <category android:name="android.intent.category.HOME" /> 
         </intent-filter> 
    </receiver> 

時々category.HOMEはそうあなたもcategory.DEFAULTを使用することができます動作しませんが..私は、この背後にある理由を知りませんしかし、そのは細かい作業..

0

のAndroidManifest.xml

<receiver android:name=".Receiver"> 
     <intent-filter android:priority="1000000"> 
      <action android:name="android.intent.action.ACTION_SHUTDOWN" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
     </intent-filter> 
    </receiver> 

Receiver.xml

public class Receiver extends BroadcastReceiver { 

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

if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) { 

     **do your stuff at here...** 

    } 
} 
関連する問題