2012-07-09 73 views
2

如何在當時調用方法從服務到活動。我想只使用定時器函數和處理函數來調用服務.am。如何從服務中調用活動方法

在我的活動方法名是SAVEDATA(),我想CAL此功能

服務

public class MyService extends Service 
{ 
    @Override 
    public IBinder onBind(Intent intent) 
    { 
     return null; 
    } 

    @Override 
    public void onCreate() 
    { 
     Log.d(TAG, "onCreate"); 
    } 

    @Override 
    public void onDestroy() 
    { 
     Log.d(TAG, "onDestroy"); 
    } 

    public void onStart(Intent intent, int startid) 
    { 
     Timer mTimer = new Timer(user); 
     mTimer.scheduleAtFixedRate(new mainTask(), 5000,60000);//1 hour=3600 s 

    } 

    private class mainTask extends TimerTask 
    { 
     public void run() 
     { 
      toastHandler.sendEmptyMessage(0); 
     } 
    } 


    private final Handler toastHandler = new Handler() 
    { 
     public void handleMessage(Message msg) 
     { 
       Intent myIntent = new Intent(getBaseContext(),StorageHelper.class); 
       myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(myIntent);//here i start the activity but i need to acces the particular method only. 

     } 
    };  

} 

更新

活動

public class StorageHelper extends Activity 
{ 
    final DBAdapter1 database=new DBAdapter1(this); 

MessageCount objmsgCount=new MessageCount(); 
String msgCount; 
int count; 
String []tokens=null; 
String notify=null; 
int userid=71; 


public String savedata() 
    { 
     msgCount=objmsgCount.getMessageCount(); 
     { 
      try { 
       database.open(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      } 
        long id=database.insert(71,4,"yes"); 
        database.close(); 

     return "success"; 
    } 
} 
+0

我的問題是CAL的方法,而不是在我的活動中啓動活動 – JavaH 2012-07-09 06:40:47

+0

我插入,刪除並更新值,所以我需要cal方法separetly – JavaH 2012-07-09 06:52:39

+0

你聲明StorageHelper活動在你的清單文件? – 2012-07-09 06:57:03

回答

0

這是一個很好的問題,可能以前曾經被問過很多次,但是有一個讓我難以置信。

您有幾種選擇,其中最簡單的是與你的活動註冊一個監聽器,但是這需要你實現onBind(意圖)這樣你就可以從活動連接到服務,所以你可以註冊監聽器。

下面的例子展示瞭如何做到這一點,一旦你已經註冊的活動作爲一個聽者setServiceClient(ExampleServiceClient)那麼服務可以調用方法exampleServiceClientMethod()上的活動。

您會注意到我在註冊客戶端時使用WeakReference,請務必在調用添加到ExampleServiceClient的任何方法時檢查是否仍然有引用。

public class ExampleService extends Service { 

    public interface ExampleServiceClient { 
     void exampleServiceClientMethod(); 
    } 

    private WeakReference<ExampleServiceClient> mClient; 

    public void setServiceClient(ExampleServiceClient client) { 
     if(client == null) { 
      mClient = null; 
      return; 
     } 

     mClient = new WeakReference<ExampleServiceClient>(client); 
    } 

    public class ExampleBinder extends Binder { 
     ExampleService getService() { 
      return ExampleService.this; 
     } 
    } 

    private IBinder mBinder = new ExampleBinder(); 

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

您的活動:

public class ExampleServiceActivity extends Activity implements ExampleServiceClient { 

    private ExampleServiceConnection mServiceConnection = new ExampleServiceConnection(); 
    private ExampleService mService = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     bindService(new Intent(this, ExampleService.class), mServiceConnection, BIND_AUTO_CREATE); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 

     unbindService(mServiceConnection); 
    } 


    class ExampleServiceConnection implements ServiceConnection { 

     public void onServiceConnected(ComponentName name, IBinder service) { 
      mService = ((ExampleBinder)service).getService(); 

      mService.setServiceClient(ExampleServiceActivity.this); 
     } 

     public void onServiceDisconnected(ComponentName name) { 
      mService.setServiceClient(null); 
      mService = null; 

     } 
    } 


    public void exampleServiceClientMethod() { 
     // TODO Auto-generated method stub 
    } 
} 

希望有所幫助。

+0

我更新我的活動代碼從我的服務類我需要cal的savedata函數 – JavaH 2012-07-09 07:00:53

+0

您的問題的答案是在我的示例中,您需要將您的活動與您的服務,這是我的例子做了什麼。 – 2012-07-09 07:06:48

+0

從你的例子中你從活動中調用什麼方法?它很難理解 – JavaH 2012-07-09 07:10:18

0

我想你錯了。如果您不想啓動Activity,但只想調用其方法,則最好將此方法推出「活動」。有很多責任的班級被認爲是不好的設計。 也考慮使用Application類。

0

有三種方法可以處理來自Activity的服務。

  • 的IBinder實施
  • 使用信使
  • 使用AIDL

最簡單的方式恕我直言,是使用綁定服務

public class Server extends Service{ 

IBinder mBinder = new LocalBinder(); 

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

public class LocalBinder extends Binder { 
    public Server getServerInstance() { 
    return Server.this; 
    } 
} 

public String getTime() { 
    SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    return mDateFormat.format(new Date()); 
} 
} 



package com.example.bindservice.binder; 

import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.example.bindservice.binder.Server.LocalBinder; 

public class Client extends Activity { 

boolean mBounded; 
Server mServer; 
TextView text; 
Button button; 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     text = (TextView)findViewById(R.id.text); 
     button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
    text.setText(mServer.getTime()); 
    } 
    }); 
    } 

@Override 
protected void onStart() { 
    super.onStart(); 
    Intent mIntent = new Intent(this, Server.class); 
     bindService(mIntent, mConnection, BIND_AUTO_CREATE); 
}; 

ServiceConnection mConnection = new ServiceConnection() { 

    public void onServiceDisconnected(ComponentName name) { 
    Toast.makeText(Client.this, "Service is disconnected", 1000).show(); 
    mBounded = false; 
    mServer = null; 
    } 

    public void onServiceConnected(ComponentName name, IBinder service) { 
    Toast.makeText(Client.this, "Service is connected", 1000).show(); 
    mBounded = true; 
    LocalBinder mLocalBinder = (LocalBinder)service; 
    mServer = mLocalBinder.getServerInstance(); 
    } 
}; 

@Override 
protected void onStop() { 
    super.onStop(); 
    if(mBounded) { 
    unbindService(mConnection); 
    mBounded = false; 
    } 
}; 
} 
相關問題