gpt4 book ai didi

java - Android 服务创建单例类的新实例

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:31:47 25 4
gpt4 key购买 nike

我正在使用单例类通过应用程序(即一些队列)保存一些数据。

我正在创建我的 Application 类的 Singleton 类实例 onCreate 方法。

 @Override
public void onCreate() {
super.onCreate();
mInstance = this;
mContext = getApplicationContext();
Queue.getInstance(); // this is my singleton class instance
}

在此之后,我将在我的 Activity 中的这个单例类中添加数据

Queue.getInstance().addItem(qItem);
Log.d(Constants.TAG, "Added Item Queue Size: "+Queue.getInstance().getQueueList().size());

直到一切正常。我可以在我的 ActivitiesListView Adapter 中访问数据,但是当我启动服务并尝试访问服务的数据 onCreate

Log.d(Constants.TAG, "Playing Item Queue Size: "+Queue.getInstance().getQueueList().size()+" Current Item No. "+Queue.getInstance().getCurrentPlayingItem());
String url = Queue.getInstance().getQueueItem(Queue.getInstance().getCurrentPlayingItem()).getLinkUrl();

我的单例实例变为空,我的单例创建了新实例。这导致我在服务中丢失了数据。

以下是我的错误流程。

  1. 开始应用程序创建实例 - 工作
  2. 从 Activity 和适配器添加数据 - 工作
  3. 启动服务并访问数据 - 不工作,因为单例实例在服务内部变为空

以下是我的单例类的代码

import java.util.ArrayList;

import com.taazi.utils.Constants;

import android.util.Log;

public class Queue {
private ArrayList<QueueItem> mQueueList;
static Queue mInstance;
private int currentPlayingItem=0;

private Queue(){
mQueueList = new ArrayList<QueueItem>();
}

public static Queue getInstance(){
if(mInstance == null){
mInstance = new Queue();
Log.d(Constants.TAG, "New Instance");
}
return mInstance;
}

public void addItem(QueueItem item){
mQueueList.add(item);
}

public void removeItem(int position){
mQueueList.remove(position);
}

public ArrayList<QueueItem> getQueueList(){
return mQueueList;
}

public QueueItem getQueueItem(int position){
return mQueueList.get(position);
}

public int getCurrentPlayingItem() {
return currentPlayingItem;
}

public void setCurrentPlayingItem(int currentPlayingItem) {
this.currentPlayingItem = currentPlayingItem;
}


}

AudioPlayBackService.Java

package com.taazi.services;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

import com.taazi.helper.NotificationHelperNew;
import com.taazi.models.Queue;
import com.taazi.models.QueueItem;
import com.taazi.utils.Constants;

public class AudioPlayBackService extends Service {

/**
* Called to go toggle between pausing and playing the music
*/
public static final String TOGGLEPAUSE_ACTION = "com.taazi.services.togglepause";

/**
* Called to go to pause the playback
*/
public static final String PAUSE_ACTION = "com.taazi.services.pause";

/**
* Called to go to stop the playback
*/
public static final String STOP_ACTION = "com.taazi.services.stop";

/**
* Called to go to the previous track
*/
public static final String PREVIOUS_ACTION = "com.taazi.services.previous";

/**
* Called to go to the next track
*/
public static final String NEXT_ACTION = "com.taazi.services.next";

/**
* Used to build the notification
*/
private NotificationHelperNew mNotificationHelper;

@Override
public IBinder onBind(Intent intent) {
return null;
}

MediaPlayer player;

@Override
public void onCreate() {
super.onCreate();
// Initialize the notification helper
mNotificationHelper = new NotificationHelperNew(this);

Log.d(Constants.TAG, "Playing Item Queue Size: "+Queue.getInstance().getQueueList().size()+" Current Item No. "+Queue.getInstance().getCurrentPlayingItem());
String url = Queue.getInstance().getQueueItem(Queue.getInstance().getCurrentPlayingItem()).getLinkUrl();
player = MediaPlayer.create(this, Uri.parse(url));
player.setLooping(false); // Set looping
updateNotification();
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}

public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}

public void onStop() {

}
public void onPause() {

}
@Override
public void onDestroy() {
mNotificationHelper.killNotification();
player.stop();
player.release();
}

@Override
public void onLowMemory() {

}

/**
* Updates the notification, considering the current play and activity state
*/
private void updateNotification() {
QueueItem item = Queue.getInstance().getQueueItem(Queue.getInstance().getCurrentPlayingItem());
mNotificationHelper.buildNotification("", item.getArtist(),
item.getTitle(), (long)50, null, true);
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.taazi.android"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="20" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.GET_TASKS" />


<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

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

<application
android:name="com.taazi.app.AppController"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Apptheme" >
<activity
android:name="com.taazi.activities.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- Music service -->
<service
android:name="com.taazi.services.AudioPlayBackService"
android:label="@string/app_name"
android:process=":main" />

</application>

</manifest>

最佳答案

您正在不同的进程中运行您的Service,这与您的Application 上下文不同,这就是队列由null 在新进程中。

从 list 中的 Service 中删除以下内容,您就可以开始了:

android:process=":main"

此外,我建议您在服务中使用 HandlerThread 来卸载一些繁重的操作。

关于java - Android 服务创建单例类的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24625638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com