我有一个扩展插件(PhoneGap)的Java类,但是在该类中,我调用了另一个扩展Activity的类,它只是行不通!我的意思是,它似乎没有被调用。为了确认这一点,这次我更改了第二节课,没有从Activity扩展,它可以正常工作。我需要第二个来扩展Activity,因为我正在使用这两个实用程序(getFileStreamPath和openFileOutput)来创建文件
文件filepath = getFileStreamPath(“ filename.CPCL”); 和openFileOutput FileOutputStream os = this.openFileOutput(fileName,Context.MODE_PRIVATE);
我有一个带有扩展了自定义的类的应用程序,该类Service
调用了另一个扩展类Activity
。
首先,我实例化活动。在onCreate
您的Plugin类中使用:
// get a handle on your Application
Application app = getApplication();
Intent intent = new Intent(getApplicationContext(), YourActivity.class);
app.startActivity(intent);
这将启动您的活动并调用标准的生命周期事件。
我处理与正在运行的Activity的持续通信的方式是使用Handler
从您的插件发送广播,Activity在其接收器中接收广播。在onCreate
您的插件的中:
mHandler.post(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Call the Activity");
Intent intent = new Intent(YourActivity.CALL_FROM_PLUGIN);
intent.putExtra("request", <<Any extras you might want to send through>>);
sendBroadcast(intent);
}
});
在活动中,我声明变量:
public static final String CALL_FROM_PLUGIN= "CALL_FROM_PLUGIN";
然后在其中onCreate()
添加以下内容;
IntentFilter filter = new IntentFilter();
filter.addAction(CALL_FROM_PLUGIN);
registerReceiver(mBroadcastReceiver, filter);
然后实施BroadcastReceiver
:
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "BroadcastReceiver.onReceive()");
if (CALL_FROM_PLUGIN.equals(action)) {
Log.d(TAG, "Received call from Plugin");
// DO YOUR STUFF HERE
}
}
};
从框架的角度来看,其他人也许可以指出为什么这样做是必要的,但这是我理解应该调用“活动”的方式。我希望这与您的服务类一样适用于您的插件类!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句