Android构建前台服务,以及PendingIntent返回到当前活动的解决办法

刚开始编写的时候在处理PendingIntent时,就是简单的让所打开的intent回到活动,但是后来发现这样实际上只是新建了一个相同的活动覆盖住了正在进行的活动上,就像写的音乐播放器,在后台的时候若我点击通知栏里的前台服务,会打开了一个新的音乐播放器活动覆盖在正在播放音乐的活动的音乐上面,并不是把后台正在进行的音乐活动调出来。最后解决方法如下:

在AndroidManifest.xml中修改当前活动为singleTask或者singleTop

<activity
            android:name=".MusicPlay"
            android:launchMode="singleTask">
</activity>
这样点击HOME键活动后台运行,点击前台服务不会new出一个新的活动。
前台服务的构建方法:重写Service的onCreate()和onDestroy()方法

@Override
    public void onCreate(){
        super.onCreate();
        Intent nfIntent = new Intent(this, MusicPlay.class);
        PendingIntent pi = PendingIntent.getActivity(this,0,nfIntent,0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("Dance团音乐播放")
                .setContentText(" 正在播放,点击返回音乐")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.music)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.drawable.dance))
                .setContentIntent(pi)
                .build();
        startForeground(1,notification);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }
启动服务:

//启动前台服务
        Intent ServiceIntent = new Intent(this,MusicService.class);
        startService(ServiceIntent);
销毁服务:

//销毁前台服务
                Intent intent = new Intent(this,MusicService.class);
                stopService(intent);





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们需要在Android应用中执行一些长时间运行的任务时,可以使用前台服务前台服务是一种在通知栏中显示持续运行通知的服务,以提醒用户应用正在后台执行任务。 以下是一个简单的Android前台服务的例子: 1. 创建一个Service类,继承自android.app.Service,并重写onCreate()、onStartCommand()和onDestroy()方法。 ```java public class MyForegroundService extends Service { private static final int NOTIFICATION_ID = 1; private static final String CHANNEL_ID = "ForegroundServiceChannel"; @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { createNotificationChannel(); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Foreground Service") .setContentText("Running...") .setSmallIcon(R.drawable.ic_notification) .setContentIntent(pendingIntent) .build(); startForeground(NOTIFICATION_ID, notification); // 执行长时间运行的任务 return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); stopForeground(true); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "Foreground Service Channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } } ``` 2. 在AndroidManifest.xml文件中注册前台服务。 ```xml <service android:name=".MyForegroundService" android:enabled="true" android:exported="false" /> ``` 3. 在需要启动前台服务的地方,使用以下代码启动服务。 ```java Intent serviceIntent = new Intent(this, MyForegroundService.class); ContextCompat.startForegroundService(this, serviceIntent); ``` 这样,当启动前台服务时,会在通知栏中显示一个持续运行的通知,告知用户应用正在后台执行任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值