2014-02-18 32 views
0

我想创建一个服务,我在5000ms后反复调用一个方法。这种方法递增计数器,当它达到6时,我想通过广播接收器调用另一个活动。 但是我收到一个错误“你是否错过了一个对unregisterreceiver()的调用?”谁能告诉我究竟是什么问题?服务和广播接收器

主要活动

public class ServicesExampleActivity extends Activity implements OnClickListener 
{ 
Button start,stop; 
IntentFilter intentfilter; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_services_example); 

    intentfilter = new IntentFilter(); 
    intentfilter.addAction("LOCATION_REACHED"); 


    registerReceiver(intentreceiver, intentfilter); 

    start = (Button) findViewById(R.id.start); 
    stop = (Button) findViewById(R.id.stop); 

    start.setOnClickListener(this); 
    stop.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) 
{ 
    if(v.getId()==R.id.start) 
    { 
     startService(new Intent(getApplicationContext(),ServiceClass.class)); 
    } 
    else if(v.getId()==R.id.stop) 
    { 
     stopService(new Intent(getApplicationContext(),ServiceClass.class)); 
    }  
} 

private BroadcastReceiver intentreceiver = new BroadcastReceiver() 
{ 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 

     if(intent.getAction().equals("LOCATION_REACHED")) 
     { 
      Intent intent1 = new Intent(getApplicationContext(),LocationReached.class); 
      startActivity(intent1); 
     } 
    } 
}; 


} 

ServiceClass.java

public class ServiceClass extends Service 
{ 
int counter = 0; 
static final int UPDATE_INTREVAL = 5000; //in milliseconds 
private Timer timer = new Timer(); 

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

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show(); 
    doSomeThingRepeatedly(); 
    return START_STICKY; 
} 

@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    if(timer!=null) 
    { 
     timer.cancel(); 
    } 
    Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show(); 
} 

private void doSomeThingRepeatedly() 
{ 
    timer.scheduleAtFixedRate(new TimerTask() 
    { 

     @Override 
     public void run() 
     { 

      Toast.makeText(getApplicationContext(), ""+counter, Toast.LENGTH_SHORT).show(); 
      counter++;    

      if(counter==6) 
      { 

       Intent broadcastintent = new Intent(); 
       broadcastintent.setAction("LOCATION_REACHED"); 
       getApplicationContext().sendBroadcast(broadcastintent); 
      } 
     } 
    }, 0, UPDATE_INTREVAL); 
} 

} 

andridmanifest.xml

<activity 
     android:name="com.vallabh.servicesexample.ServicesExampleActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <service android:name="com.vallabh.servicesexample.ServiceClass"></service> 
    <activity android:name="com.vallabh.servicesexample.LocationReached"></activity> 

回答

2

“你缺少unregisterReceiver()的调用”

表示当Activity未运行时,您忘记取消注册BroadcastReceiver。在活动中使用onResume的方法来注册广播的onCreate

@Override 
protected void onStop() 
{ 
    if(null !=intentreceiver) 
     unregisterReceiver(intentreceiver); 

    super.onStop(); 
} 

,而是注册广播接收器的,因为当你来到从明年活动回来然后再重新注册:所以用活动的onStop()方法来注销广播接收器作为。

+0

好吧,帮助,但现在我得到一个新的错误“无法创建处理程序内部线程没有调用Looper.prepare()”。 –

+0

@VallabhLakade:只是评论'Toast.makeText(getApplicationContext(),“”+ counter,Toast.LENGTH_SHORT).show();'因为你试图显示吐司从计时器线程 –

+0

执行没有进入run()里面timer.schedeleAtFixedRate –

0

您应该使用unregisterReceiver(theReceiver)重载Activity.onPause()并取消注册您注册的接收方;