2014-07-05 100 views
0

我对安卓服务有问题 当我关闭应用程序时,主要活动关闭并且服务被重新创建 - 创建方法 自动调用并且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, 阿龙

回答

0

看看这有助于 - 使用共享偏好节省一些字符串的状态,并检索回来时重新开放的活动。

+0

我想避免使用持久性,因为据我所知,服务实例不假设要销毁直到停止服务调用,是不是这样? – Aloni

0

使用这种方法在服务类

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // We want this service to continue running until it is explicitly 
     // stopped, so return sticky. 

     // do your logic here not in onStart(). 

     return START_NOT_STICKY; 
    } 

START_NOT_STICKY和START_STICKY

这里漂亮的答案START_NOT_STICKY和START_STICKY

https://stackoverflow.com/a/9441795/942224

,如果你想停止服务时,应用比在0中使用stopService更接近

+0

当我这样做时,服务在活动结束时被销毁。 – Aloni

+0

从onDestroy中删除stopService。所以如果你从最新版本中删除应用程序,服务将被破坏 –

1

您的代码的第一个问题是您允许多次启动MyServcie类中的线程。每个用户按下“开始”按钮时,线程start方法被调用,根据Java API specification这是违法的:

这是从来没有的法律,以启动一个线程不止一次。特别是,线程一旦完成执行就不会重新启动。

您应该防止重复启动您的Service类中的线程,例如通过使用标志来告诉线程是否已启动。

另请注意,Service#onStart方法自API级别5开始被弃用。相反,如果可能,应该使用Service#onStartCommand。 如Sanket指出的那样,如果您只想执行一次MyThread#run方法的代码,则应该返回START_NOT_STICKY。你也可能想明确地停止finally块中的服务。

相关问题