2016-07-04 2 views
1

J'utilise un récepteur de diffusion pour démarrer automatiquement mon application lorsque le téléphone est allumé. Mais ça ne marche pas dans mon cas. Je joins les détails du programme. S'il vous plaît quelqu'un m'aider, où suis-je à la traîne. Merci à l'avancereciever ne fonctionne pas lorsque le téléphone est redémarré

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

mis enable = true dans votre récepteur. –

+0

bonjour sagar, vous voulez dire dans le fichier manifeste –

+0

oui .. vous pouvez voir ma réponse. –

Répondre

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

et dans votre fichier manifeste:

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

.. Je ne parfois category.HOME fonctionnent pas pour que vous puissiez utiliser category.DEFAULT aussi connaître la raison derrière cela, mais son travail bien ..

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

    } 
}