如何在當時調用方法從服務到活動。我想只使用定時器函數和處理函數來調用服務.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";
}
}
我的問題是CAL的方法,而不是在我的活動中啓動活動 – JavaH 2012-07-09 06:40:47
我插入,刪除並更新值,所以我需要cal方法separetly – JavaH 2012-07-09 06:52:39
你聲明StorageHelper活動在你的清單文件? – 2012-07-09 06:57:03