2014-02-10 72 views
0

我想從Activity對象中調用Service對象中的方法,但是我發現無法正常調用MainActivity中的方法。來自活動服務的Android調用方法

我希望我的代碼解釋了我的意思是更好:

服務:

public class Timer extends Service { 

public Vibrator v; 
public MainActivity ma; 
public CountDownTimer mycounter; 
public static final String MY_SERVICE = "de.example.timer.MY_SERVICE"; 

public IBinder onBind(Intent arg0) 
{ 
     return null; 
} 

public void onCreate() 
{ 
     super.onCreate(); 
     ma = new MainActivity(); 
     v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
     startService(); 
} 

public void startService() 
{ 
    mycounter = null; //delete counter 
    mycounter = new CountDownTimer(5000, 100){ 
     public void onTick(long millisUntilFinished) { 
      //ma.timer.setText(ma.formatTime(millisUntilFinished+1000)); 
      //ma.builder.setContentText("Timer: " + ma.formatTime(millisUntilFinished+1000)); //update Timer 
      //ma.notificationManager.notify(MainActivity.MY_NOTIFICATION_ID, ma.builder.build()); 

//It is not possible to call a methode this way in a service.. 
     } 

     public void onFinish() { 
      //ma.timer.setText("00:00:00"); 
      v.vibrate(1000); 
      mycounter.cancel(); 
      mycounter.start(); 
     } 
    }; 
    mycounter.start(); 
} 

public void onDestroy() 
{ 
     super.onDestroy(); 
     mycounter.cancel(); 


    } 
} 

活動:

public class MainActivity extends Activity { 

private ImageButton imagebutton; 
public Vibrator v; 
public TextView timer; 
public boolean pressed; 
public String output; 
public long waitingtime; 
public Timer service; 

public static final int MY_NOTIFICATION_ID = 1; 
public NotificationManager notificationManager; 

public Context context; 
public Builder builder; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 

    pressed = false; 
    waitingtime = 600000; // Standartmäßig auf 10 min 

    v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 

    imagebutton = (ImageButton) findViewById(R.id.imageButton1); 
    imagebutton.setBackgroundResource(R.drawable.start); 
    timer = (TextView) findViewById(R.id.timer); 
    timer.setText(formatTime(waitingtime)); 

    Intent intent = new Intent (this, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0); 
    notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    context = getApplicationContext(); 
    builder = new NotificationCompat.Builder(context) 
    .setSmallIcon(R.drawable.notlogosmall) 
    .setContentTitle("Start!") 
    .setContentText("Timer set to " + waitingtime/1000 + " seconds") 
    .setContentIntent (pend) 
    .setTicker("Go!") 
    .setWhen(System.currentTimeMillis()) 
    .setDefaults(0) 
    .setAutoCancel(false) 
    .setOngoing(true); 
} 

public void press(View view){ 
    if(pressed == false){ 
     imagebutton.setBackgroundResource(R.drawable.stop); 
     pressed = true; 
     notificationManager.notify(MY_NOTIFICATION_ID, builder.build()); //Notification 
     v.vibrate(100); 
     hidebuttons(); 
     startService(new Intent(Timer.MY_SERVICE)); 
    } 
    else{ 
     imagebutton.setBackgroundResource(R.drawable.start); 
     pressed = false; 
     notificationManager.cancel(1); 
     timer.setText(formatTime(waitingtime)); 
     showbuttons(); 
     stopService(new Intent(Timer.MY_SERVICE)); 
     v.vibrate(100); 
    } 
} 

我如何可以調用一個對象的方法從另一個類的一個對象?

回答

0

simpke方法是從Activity發送一個intent並在Service的onStartCommand()方法中處理它。不要忘了用正確的行動/額外提供意圖&檢查,在onStartCommand()

編輯:

活動:

添加一個私有類:

private class CustomReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(ACTION_CUSTOM_ACTION)) { 
      doCustomAction(); 
     } 
    } 
} 

添加私人字段:

private CustomReceiver mCustomReceiver; 

在onCreate()方法:

mCustomReceiver = new CustomReceiver(); 

在的onResume()或其他生命週期方法:

IntentFilter filter = new IntentFilter(ACTION_CUSTOM_ACTION); 
registerReceiver(mCustomReceiver , filter); 

在的onPause()或成對(到之前的步驟)的生命週期方法的其它

unregisterReceiver(mCustomReceiver); 

活動時間你想打電話給我使用服務方式:

startService(new Intent(SOME_ACTION)); 

在服務:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    if (intent == null) { 
     return START_STICKY; 
    } 

    String action = intent.getAction(); 

    if (action == null) { 
     return START_STICKY; 
    } else if (action.equals(SOME_ACTION)) { 
        invokeSomeServiceMethod();// here you invoke service method 
      } 

    return START_STICKY; 
} 

注意,這START_STICKY可能不是你的最佳選擇,閱讀有關文件模式。

然後,當你想通知你完成活動,請致電:

startActivity(ACTION_CUSTOM_ACTION); 

這將觸發廣播reciever在那裏你可以處理完成的事件。

似乎這是很多代碼,但真的沒什麼困難。

+0

你能舉個例子嗎? – xxjaylxx

+0

添加一些代碼到我的答案 – Sam