2016-07-04 2 views
1

Я использую приемник Broadcast для автоматического запуска моего приложения, когда телефон включен. Но это не работает в моем случае. Я прикрепляю детали программы. Пожалуйста, помогите мне, где я отстаю. Заранее спасибоприемник не работает при перезапуске телефона

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

положить включить = истина в вашем приемнике. –

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

    } 
} 
Смежные вопросы