`
luckfox
  • 浏览: 64560 次
  • 性别: Icon_minigender_1
  • 来自: 台灣
社区版块
存档分类
最新评论

在Activity下以Intent切換Serivce

阅读更多
總共有兩個java檔案,ServiceDemo_Activity.java & MySerivce.java,在ServiceDemo_Activity.java中發送intent去啟動/關閉Service,
但是否有辦法在知道此Service已經驅動,則就不啟動(只有在未啟動時才啟動)?

//-----------------ServiceDemo_Activity.java---------------------
package android.demo.servicedemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ServiceDemo_Activity extends Activity {
    /** Called when the activity is first created. */
    private static final String m_TAG="ServiceDemo";
	Button m_Button_Start,m_Button_Stop;
	Context m_Context=null;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        m_Button_Start=(Button)findViewById(R.id.button0);
        m_Button_Stop=(Button)findViewById(R.id.button1);
        m_Context=this.getBaseContext();
        m_Button_Start.setOnClickListener(on_Button_Start_Click);
        m_Button_Stop.setOnClickListener(on_Button_Stop_Click);
        Log.d(m_TAG, "onCreate"); 
    };
    public OnClickListener on_Button_Start_Click=new OnClickListener()
    {

		@Override
		public void onClick(View v)
		{
			// TODO Auto-generated method stub
			Intent intent=new Intent(ServiceDemo_Activity.this,MyService.class);
			startService(intent); 
			Log.d(m_TAG, "on_Button_Start_Click"); 
		}
    	
    };
    public OnClickListener on_Button_Stop_Click=new OnClickListener()
    {

		@Override
		public void onClick(View v)
		{
			// TODO Auto-generated method stub
			Intent intent=new Intent(ServiceDemo_Activity.this,MyService.class);
			stopService(intent);
			Log.d(m_TAG, "on_Button_Stop_Click"); 
		}
    	
    };
}

//-----------------MyService .java---------------------
package android.demo.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service
{
	private static final String TAG="MyService";
	MediaPlayer player;
	@Override
	public IBinder onBind(Intent intent)
	{
		// TODO Auto-generated method stub
		return null;
	}
	@Override 
	public void onCreate() 
	{ 
      
		Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 
		Log.d(TAG, "onCreate"); 
              
		//player = MediaPlayer.create(this, R. raw.braincandy);//
		//player.setLooping(false); // Set looping 
	} 
	@Override 
	public void onDestroy()
	{
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
        Log.d(TAG, "onDestroy"); 

	}
    @Override 
    public void onStart(Intent intent, int startid) 
    { 
    	Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
    	Log.d(TAG, "onStart"); 
    	//player.start(); 
    } 


}
//---------------------------AndroidManifest.xml-----------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="android.demo.servicedemo"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ServiceDemo_Activity"
                  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:enabled="true" android:name=".MyService" /> 

    </application>
    <uses-sdk android:minSdkVersion="7" />

</manifest> 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics