重新启动手机时,接收器无法正常工作

时间:2016-07-04 06:17:51

标签: android broadcastreceiver restart

我正在使用广播接收器在手机开机时自动启动我的应用程序。但它不适用于我的情况。我附上了计划细节。请有人帮助我,我在哪里落后。提前致谢

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


    }
}

2 个答案:

答案 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也..我不知道这背后的原因,但它的工作正常..

答案 1 :(得分: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...**

    }
}
相关问题