服务方法在android中不起作用

时间:2012-12-06 19:36:14

标签: android android-service toast

我刚刚创建了一个服务,如下所示:

package com.example.timepass;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class alarm extends Service{


@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Toast.makeText(this, "Entered in service", Toast.LENGTH_SHORT).show();

}

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

         Toast.makeText(this, "onStartCommand...", Toast.LENGTH_LONG).show();
         return 1; 

        // Log.i("YourService", "Yes this works.");
     }
   @Override
   public void onDestroy() {
       super.onDestroy();
       Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
   }

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Changed", Toast.LENGTH_SHORT).show();
    return null;
}



}

现在,当我通过以下命令从mainactivity启动服务时:

Intent myIntent = new Intent("com.example.timepass.ALARM");
MainActivity.this.startService(myIntent);

通过这样做没有错误,但没有任何TOAST of Service类被dipslayed

我的清单是:

<service class=".alarm" android:name=".alarm" android:enabled="true">
  <intent-filter>
   <action android:value="com.example.timepass.ALARM"
               android:name=".alarm" />

       </intent-filter>
   </service>

请指导我!!!

1 个答案:

答案 0 :(得分:1)

可能您没有service in your manifest,或者没有与您的操作匹配的内容。检查LogCat应该会发出一些可能有帮助的警告。

更有可能的是,您应该通过以下方式启动服务:

startService(new Intent(this, alarm.class));
相关问题