我對安卓服務有問題 當我關閉應用程序時,主要活動關閉並且服務被重新創建 - 創建方法 自動調用並且onstart也被稱爲自動 - 所有狀態都是不見了。當主要活動被破壞時重新創建Android服務
這是我的活動代碼
public class ServicesDemo extends Activity implements OnClickListener
{
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop;
@Override
public void onCreate(Bundle savedInstanceState)
{
if (savedInstanceState != null)
{
Log.d(TAG, "ServicesDemo:onCreate WITH savedInstanceState)");
}
Log.d(TAG, "ServicesDemo:onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src)
{
switch (src.getId())
{
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
// Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
}
}
}
這是服務代碼:
public class MyService extends Service
{
private static final String TAG = "ServicesDemo";
private static MyThread t = new MyThread();
static int yy = 90;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
// MockGPSLocationModel.getInstance().Counter++;
// Log.d(TAG, "MockGPSLocationModel.getInstance().Counter :: " +
// MockGPSLocationModel.getInstance().Counter);
// MockGPSLocationModel.getInstance().Counter++;
}
static public class MyThread extends Thread
{
MediaPlayer player;
public Context ctx;
@Override
public void run()
{
try
{
for (int i = 0; i < 100; i++)
{
Log.i(TAG, "lOOP - " + i);
Thread.sleep(2000);
}
player = MediaPlayer.create(ctx, R.raw.braincandy);
player.setLooping(false); // Set looping
player.start();
}
catch (Exception e)
{
Log.e(TAG, e.toString());
}
finally
{
}
}
}
@Override
public void onDestroy()
{
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
// player.stop();
}
@Override
public void onStart(Intent intent, int startid)
{
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
t.ctx = this;
t.start();
// player.start();
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".ServicesDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true" />
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
請幫助。 我堅持了那3天。
THX, 阿龍
我想避免使用持久性,因爲據我所知,服務實例不假設要銷燬直到停止服務調用,是不是這樣? – Aloni