当时如何调用方法 从服务到活动。我只想使用计时器函数和处理程序从服务 .am 调用特定方法。
在我的活动中,方法名称是 savedata(),我想调用这个函数
服务
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";
}
}