【问题标题】:From the notificationbar, to call the working Class从通知栏,调用工人阶级
【发布时间】:2014-11-07 00:13:30
【问题描述】:

我正在开发一个在线广播应用程序。该应用程序在后台运行。当我单击 NotificationManager 时,radioclass 再次开始工作。我想调用正在运行的无线电类。我该怎么办?

player = MediaPlayer.create(this, Uri.parse("http://...../playlist.m3u8"));
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer player) {

                }
            });
            player.start();



final int notificationID = 1234;
        String msg = "mesajjj";
        Log.d(TAG, "Preparing to update notification...: " + msg);

        NotificationManager mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);



        Intent intent = new Intent(this, RadioClass.class);

//这里 RadioClass 再次运行。但是 RadioClass 已经在运行。我应该调用正在运行的 RadioClass。

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.logotam)
                .setContentTitle("Test FM")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);

        mBuilder.setContentIntent(pendingIntent);
        mNotificationManager.notify(notificationID, mBuilder.build());

【问题讨论】:

    标签: java android notificationmanager


    【解决方案1】:

    如果你想回到你当前正在运行的同一个活动,你可以这样做:

    在您的 AndroidManifest.xml 中添加以下标签:

    <activity
    ........
        android:launchMode="singleTop" >
    ........
    </activity>
    

    像这样修改你的 PendingIntent:

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    

    通过此修改,您可以确保您的活动在任何给定时间都只有一个实例。在您的活动类上,您还可以覆盖 onNewIntent 方法:

        @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    
    }
    

    此方法将处理对您的活动的所有额外调用。

    【讨论】:

    • 完美!非常感谢。
    猜你喜欢
    • 2015-05-17
    • 2017-02-23
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2015-04-26
    • 1970-01-01
    相关资源
    最近更新 更多