我正在做一些我认为很简单的事情,就像过去在 2.3.4 机器人上做过广播接收器一样。我现在在这个小程序中使用 4.x。
这是主要的架构/流程。我希望/有一个接收器来监听自定义广播/意图。我创建了这个作为项目/apk的立场
public class FileIntegrityMonitor extends BroadcastReceiver
{
public String CLASS_NAME = "FileIntegrityMonitor";
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(CLASS_NAME, "Entered onReceive: got broadcast from startFIM");
String actionReceived = intent.getAction();
Log.d(CLASS_NAME, "Action received=" + actionReceived);
if (actionReceived.equals(StartFIMActivity.CUSTOM_INTENT))
{
//start service to perform the file integrity check
Log.d(CLASS_NAME, "start service to perform file integrity check");
}
}
然后我创建了一个活动作为一个单独的项目/应用程序作为驱动程序,以启动广播接收器。以后可能会用其他东西代替,但现在想把机制/通讯搞定。public class StartFIMActivity extends Activity { /** 首次创建活动时调用。*/ public static final String CUSTOM_INTENT = "com.kdms2.StartFIM.intent.action.start"; 公共字符串 CLASS_NAME = "StartFIMActivity";
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.d(CLASS_NAME, "Entered onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent();
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); //api 3.1+
i.setAction(CUSTOM_INTENT);
Log.d(CLASS_NAME, "send brodcast to monitor");
this.sendBroadcast(i);
}
}
现在在跟踪中,我确实在 FileIntegrityMonitor 中得到了意图,但是一条奇怪的消息表明它正在尝试运行某些方法,但我不知道为什么。错误是:E/AndroidRuntime(979): java.lang.RuntimeException: 无法实例化接收器 com.kdms2.FileIntegrityMonitor.StartFIMActivity: java.lang.ClassNotFoundException: com.kdms2.FileIntegrityMonitor.StartFIMActivity
为什么它将广播动作的活动的类名添加到广播接收器?
它是清单(接收者)中的东西吗?
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".StartFIMActivity" android:enabled="true">
<intent-filter android:priority="99999999999">
<action android:name="com.kdms2.StartFIM.intent.action.start"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</manifest>
活动清单
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".StartFIMActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</application>
</manifest>
v/r,弗兰克