4

我想在运行时从 android 手机中删除 sim 卡的通知。

我的应用程序在 GingerBread 上运行,而它不在 HTC One V 上运行,而 HTC One V 运行在 ICS 及更高版本上。这是我的代码:

1) 接收类

package com.TestIt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class SimEventReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent systemIntent) {
    // TODO Auto-generated method stub
    Intent intent=new Intent(context, SimService.class);
    context.startService(intent);
}

}

2)服务等级

package com.TestIt;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class SimService extends Service {
TelephonyManager tele;
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Log.d("Kanishk", "In Service OnCreate");


    int simState = tele.getSimState();
    switch (simState)
    {
    case TelephonyManager.SIM_STATE_ABSENT:
        Log.d("kanishk", "onCreate1");
        TestItActivity.simState(this);
        break;
    case TelephonyManager.SIM_STATE_READY:
        Log.d("kanishk", "onCreate2");
        Toast.makeText(this, "Now Sim is ok", Toast.LENGTH_LONG).show();
        break;
    case TelephonyManager.SIM_STATE_UNKNOWN:
        Log.d("kanishk", "onCreate3");
        Toast.makeText(this, "not Known", Toast.LENGTH_LONG).show();
        break;
    }
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

3)活动包com.TestIt;

import android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

public class TestItActivity extends Activity {
/** Called when the activity is first created. */
private  static final String tag = "Activity";
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    Log.d(tag, "OnCreate");
    setContentView(R.layout.main);
}
public static void simState()
{
    Log.d(tag, "Sim State");
}
}

4)清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.systemEvent"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="14" />

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".SystemEventActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".BroadCastReceiverS"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

    <service android:name=".MyService" >
    </service>
</application>

</manifest>
4

1 回答 1

0

Starting with Android 3.1, all applications are set to STOPPED STATE when installed. The application is taken out of the STOPPED STATE when the user runs the application for the first time. An application is also put back in the STOPPED STATE when the user uses the application manager to manually stop it.

Since your application contains only a Service component and a BroadcastReceiver component, it is obviously never explicitly started by the user (there is no Activity for the user to start). Therefore, your application is never taken out of the STOPPED STATE.

Your BroadcastReceiver will never be run because the system does not send broadcast Intents to applications in the STOPPED STATE.

Read more about this here. Look at the section "Launch controls on stopped applications". Note particularly this quote:

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).

于 2012-10-09T20:02:48.760 回答