首页 文章

JAVAFX Gluon执行来自android活动的View

提问于
浏览
1

关于我之前关于messaging on javafx的问题,

我想在邮件到达时通知用户 . 最近我使用NotificationCompat .

当用户从通知栏触摸通知时,它必须直接打开相关视图(DirectMessageView) . 我在androidmanifest.xml中的receive标签上配置了一个活动类(NotificationActivy extends Activity),并在方法“onCreate”上调用DirectMessageView和它的presenter .

当用户在通知时触摸消息时,它不会显示DirectMessageView,但会调用presenter中的方法,并且视图不可见 . 也许这是我错误的实施,请帮忙

这是我创建的课程

Class SKSAplication that extends MobileAplication

public class SKSApplication extends MobileApplication{
   private static SKSApplication instance;
   public static final String DIRECT_MESSAGE_VIEW = "DIRECT_MESSAGE_VIEW";
   public static final String GROUP_MESSAGE_VIEW = "GROUP_MESSAGE_VIEW";
private ViewRefresh activeView;

   public SKSApplication() {
       instance = this;
   }

   public static SKSApplication getInstance() {
       return instance;
   }

@Override
public void init() {
   addViewFactory(HOME_VIEW, () -> {
        HomeView homeView = new HomeView();
        homePresenter = (HomePresenter) homeView.getPresenter();
        return (View) homeView.getView();
    });

    addViewFactory(DIRECT_MESSAGE_VIEW, () -> {
        DirectMessageView directMessageView = new DirectMessageView();
        return (View) directMessageView.getView();
    });

    addViewFactory(GROUP_MESSAGE_VIEW, () -> {
        GroupMessageView groupMessageView = new GroupMessageView();
        return (View) groupMessageView.getView();
    });

    public void doRefreshMessageUI(Object objectModel) {
    System.out.println("SKSApplication.doRefreshMessageUI " + getView().getName());
    if (getActiveView() != null)
        getActiveView().doRefresh(objectModel);
}

public ViewRefresh getActiveView() {
    return activeView;
}

public void setActiveView(ViewRefresh activeView) {
    this.activeView = activeView;
}
 }

Class MyGCMListenerService

public class MyGCMListenerService extends GcmListenerService {
private final String NOTIFICATION_TAG = "NotificationExample";

public MyGCMListenerService() {
}

@Override
public void onMessageReceived(String from, Bundle data) {
String varMessage = data.getString("message");
    try {
        JSONObject json = new JSONObject(varMessage);

        String messageContent = getStringFromJSON(json, "message");
        Integer senderId = getIntegerFromJSON(json, "senderId");
        String senderName = getStringFromJSON(json, "senderName");
        String comId = getStringFromJSON(json, "communityId");
        String salesGroup = getStringFromJSON(json, "salesGroup");
        Integer messageType = getIntegerFromJSON(json, "type");

        doViewNotification(messageType, senderName, salesGroup);
     SKSApplication.getInstance().doRefreshMessageUI(messageContent,senderId,senderName,comId );
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

private void doViewNotification(Integer messageType, String senderName, String salesGroup) {
    StringBuilder msg = new StringBuilder()
            .append("Message from ")
            .append(senderName)
            .append(" @").append(salesGroup);


    Intent resultIntent = new Intent(FXActivity.getInstance(), NotificationActivity.class);
    resultIntent.putExtra(Constants.EXTRA_INTENT.MESSAGE_TYPE.getValue(), messageType);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    FXActivity.getInstance(),
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    long[] v = {500, 1000};


    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setSound(uri)
                    .setSmallIcon(FXActivity.getInstance().getApplicationInfo().icon)
                    .setContentTitle(getApplicationName(FXActivity.getInstance().getApplicationContext()))
                    .setVibrate(v)
                    .setContentText(msg.toString())
                    .setPriority(Notification.PRIORITY_DEFAULT)
                    .setNumber(100)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(true)
                    .addAction(FXActivity.getInstance().getApplicationInfo().icon, "Action", null);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
        notificationManager.notify(NOTIFICATION_TAG, 0, builder.build());
    } else {
        notificationManager.notify(NOTIFICATION_TAG.hashCode(), builder.build());
    }

}
}

layout xml file (directmessage.fxml)

<View xmlns:fx="http://javafx.com/fxml/1" fx:id="directMessageView" prefHeight="600.0" prefWidth="400.0"
  xmlns="http://javafx.com/javafx/8.0.40"
  fx:controller="com.tenma.mobile.message.directmessage.DirectMessagePresenter">
</View>

Class DirectMessageView

public class DirectMessageView extends FXMLView {
}

Class DirectMessagePresenter

public class DirectMessagePresenter implements Initializable, ViewRefresh{
    @Override
public void initialize(URL location, ResourceBundle resources) {
    {
        directMessageView.showingProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue) {
            SKSApplication.getInstance().setActiveView(this);
            doViewMessage();
        }
   });
    }

    private void doViewMessage() {
    listMessage.getItems().clear();

    MessageStoryHelper hlp = new MessageStoryHelper();
    List<MessageModel> ls = null;
    try {
        ls = hlp.getMessages(Constants.MESSAGE_TYPE.DIRECT);
    if (ls != null && ls.size() != 0)
        for (MessageModel m :ls)
            listMessage.add(m);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
}

androidmanifest

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tenma.mobile"
      android:versionCode="1" android:versionName="1.0">
<supports-screens android:xlargeScreens="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>

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

<permission android:name="com.tenma.mobile.permission.C2D_MESSAGE"
            android:protectionLevel="signature"/>
<uses-permission android:name="com.tenma.mobile.permission.C2D_MESSAGE"/>

<application android:label="MobileSales" android:name="android.support.multidex.MultiDexApplication"
             android:icon="@mipmap/ic_launcher">
    <activity android:name="javafxports.android.FXActivity" android:label="MobileSales"
              android:configChanges="orientation|screenSize">
        <meta-data android:name="main.class" android:value="com.tenma.mobile.SKSApplication"/>
        <meta-data android:name="debug.port" android:value="0"/>
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity
            android:name="com.tenma.mobile.common.NotificationActivity"
            android:parentActivityName="javafxports.android.FXActivity">
        <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="javafxports.android.FXActivity"/>
    </activity>

    <!--start-->
    <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <!-- for Gingerbread GSF backward compat -->
            <!--<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>-->
            <category android:name="com.tenma.mobile"/>
        </intent-filter>
    </receiver>
    <!--end-->

    <service
            android:name="com.tenma.mobile.common.MyGCMListenerService"
            android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
        </intent-filter>
    </service>
</application>
    </manifest>

Class NotificationActivity extends Activity

public class NotificationActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent launchIntent = getIntent();

它工作,但没有可见的视图

SKSApplication.getInstance().switchView(SKSApplication.DIRECT_MESSAGE_VIEW);

也许使用下面的这一行,但如何设置Gluon View setContentView?或retreive视图ID和setContentView?

DirectMessageView directMessageView = new DirectMessageView();
                Parent v = directMessageView.getView();
                FXActivity.getInstance().setContentView(?????????);
        }
}

任何帮助,将不胜感激

先感谢您

1 回答

  • 0

    假设您正在运行该应用程序,这对我有用:

    将邮件发送回View后,您需要通过调用 finish() 来关闭通知活动:

    public class NotificationActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            ((SKSApplication) MobileApplication.getInstance()).doRefreshMessageUI("Hi from NotificationActivity");
    
            finish();
        }
    
    }
    

    请注意,您不需要创建 SKSApplication 的单例实例,您可以通过调用 MobileApplication.getInstance() 随时检索它的实例 .

    将消息传递给视图可以使用 Platform.runLater()SKSApplication 中完成,因为您没有在JavaFX线程上运行 .

    要检索相应的视图,只需切换到该视图:

    public void doRefreshMessageUI(String msg) {
        Platform.runLater(() -> {
            messagesView.messageProperty().set(msg);
            switchView(DIRECT_MESSAGE_VIEW);
        });
    }
    

    提供视图有一个 StringProperty ,它绑定到它的任何控件文本属性 .

    private StringProperty message = new SimpleStringProperty();
    
    public StringProperty messageProperty() {
        return message;
    }
    

相关问题