Android:为HTC One V运行Sim卡的运行时间

时间:2012-10-09 16:45:48

标签: android

我希望在运行时通知从Android手机中移除SIM卡。

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

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)的Manifest.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>

1 个答案:

答案 0 :(得分:0)

从Android 3.1开始,安装后所有应用程序都设置为 STOPPED STATE 。当用户第一次运行应用程序时,应用程序将从 STOPPED STATE 中取出。当用户使用应用程序管理器手动停止应用程序时,应用程序也会返回 STOPPED STATE

由于您的应用程序仅包含Service组件和BroadcastReceiver组件,因此用户显然从不显式启动它(用户无法启动Activity)。因此,您的应用程序永远不会被取消停止状态

您的BroadcastReceiver永远不会被运行,因为系统不会向停止状态中的应用发送广播Intent

详细了解此here。查看&#34;启动已停止应用程序控件&#34; 的部分。特别注意这句话:

  

请注意,系统会向所有人添加FLAG_EXCLUDE_STOPPED_PACKAGES   广播意图。这样做是为了防止来自后台的广播   来自无意或不必要的组件的服务   停止应用程序。后台服务或应用程序可以   通过添加FLAG_INCLUDE_STOPPED_PACKAGES来覆盖此行为   用于广播应该被允许激活停止的意图的标志   应用

     

应用程序在首次安装时处于停止状态但是   尚未启动以及何时由用户手动停止   (在管理应用程序中)。

相关问题