如何在线程中启动新服务... 线程正在连续运行,但run()中的startservice()方法没有启动... 请帮助我。
代码如下.....
package com.example.demo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class Act extends Service {
/** Called when the activity is first created. */
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(Act.this,"In On Create",Toast.LENGTH_SHORT).show();
Intent i=new Intent(getApplicationContext(),HelloService.class);
startService(i);
Toast.makeText(Act.this,"In End Create",Toast.LENGTH_SHORT).show();
updateTimeTask.start();
}
private Thread updateTimeTask = new Thread() {
public void run() {
Intent i=new Intent(getApplicationContext(),HelloService.class);
startService(i); //This Service not gets started
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:0)
你必须有一个Pending Intent:
// start something with Intent
Intent widgetUpdate = new Intent(SomeClass.this,
SomeService.class);
widgetUpdate.putExtra("Something", something);
// make this pending intent unique
widgetUpdate.setData(Uri.withAppendedPath(
Uri.parse(mUriSchemaId + "://widget/id/"),
String.valueOf(appWidgetId)));
PendingIntent newPending = PendingIntent.getService(
getApplicationContext(), 0, widgetUpdate,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteview.setOnClickPendingIntent(R.id.someview,
newPending);