gpt4 book ai didi

Android:HTC One V SIM 卡运行时移除

转载 作者:太空宇宙 更新时间:2023-11-03 11:18:33 25 4
gpt4 key购买 nike

我想要在运行时从 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) Activity 包 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) list .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>

最佳答案

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

由于您的应用程序仅包含一个Service 组件和一个BroadcastReceiver 组件,因此它显然从未被用户显式启动(没有Activity 供用户启动)。因此,您的应用程序永远不会脱离STOPPED STATE

您的BroadcastReceiver 永远不会运行,因为系统不会向处于STOPPED STATE 的应用程序发送广播Intent

阅读更多相关信息 here .查看“在已停止的应用程序上启动控件”部分。请特别注意这句话:

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

关于Android:HTC One V SIM 卡运行时移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12804676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com