如何处理来自单独模块

时间:2018-10-19 12:16:41

标签: android service module

我想创建一个在后台播放MediaPlayer的模块/库。另外,我想从ACTION_PLAY控制ACTION_PAUSENotifcation

  

服务等级

package com.xyz.library.service; //Lib

public class BackgroundSoundService extends Service {

    public static MediaPlayer mMediaPlayer;

    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();


        // Initialising MediaPlayer and setting MP3 from Raw Directory

        mMediaPlayer = MediaPlayer.create(this, R.raw.classical_flute);
        mMediaPlayer.setLooping(true); // Set looping of MP3
        mMediaPlayer.setVolume(70, 70); // Set default volume

        // MP3 Link: https://www.zedge.net/ringtone/b023aace-fa15-303b-9c74-8ff06fd3cc4e

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        mMediaPlayer.start(); // Starts MediaPlayer
        Intent notificationIntent = new Intent(this, BackgroundSoundService.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle("Service is running..")
                .setContentText("This is Content Text")
                .setContentIntent(pendingIntent);
        Notification notification = builder.build();
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
        startForeground(Integer.parseInt(NOTIFICATION_CHANNEL_ID), notification);
        return START_STICKY;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TO DO
    }

    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }


    @Override
    public void onDestroy() {
        // Stopping MediaPlayer for playing Music
        mMediaPlayer.stop();
        mMediaPlayer.release();
        stopForeground(true);
    }

    @Override
    public void onLowMemory() {

    }
}
  

UserControl的AudioModule类

package com.xyz.library.audiomodule; //Lib

public class AudioModule {

    private Context context;

    public AudioModule(Context context) {
        this.context = context;
    }

    public void playMusic() {

        Intent intent = new Intent(context, BackgroundSoundService.class);
        context.startService(intent);
    }

    public void stopMusic() {

        Intent intent = new Intent(context, BackgroundSoundService.class);
        context.stopService(intent);
    }

    public void pauseMusic(){
       BackgroundSoundService.mMediaPlayer.pause();
    }
}
  

应用程序启动器活动中的代码

package com.xyz.audiomodule;  //Application

import com.xyz.library.audiomodule.AudioModule; //Lib Imported

public class MainActivity extends AppCompatActivity{


     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAudioModule = new AudioModule(this);

    }

    public void playMusic(View view) {

        mAudioModule.playMusic(); //Service/Music is started
    }

    public void pauseMusic(View view) {

        mAudioModule.pauseMusic();  //Service/Music is paused
    }

    public void stopMusic(View view) {

        mAudioModule.stopMusic();  //Service/Music is stopped
    }
}

我的问题是

1)我想设置自定义Notifation,以便可以控制MediaPlayer Action,但我什至无法设置setContentTitle(title);,它始终显示: 标题:AppName正在运行。 消息:触摸以获取更多信息或停止该应用。  Screeshot

2)当我单击“通知”时,它显示“设置”>“ AppDetails”,但我想启动该应用程序(应用程序包装的MainActivity

请帮助我解决此问题。如果我在应用程序包装中写了Service,但不理解旧的module,则应用程序运行正常。

PS。抱歉,如果我无法正确解释。这是我对SOF的第一个问题:)

先谢谢您

0 个答案:

没有答案