Service错误

错误日志:

 Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=m4 (has extras) }
                                                                              at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1809)
                                                                              at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1838)
                                                                              at android.app.ContextImpl.startService(ContextImpl.java:1822)
                                                                              at android.content.ContextWrapper.startService(ContextWrapper.java:516)
                                                                              at myapplication.com.canvas.MainActivity.start(MainActivity.java:24)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                              at android.view.View.performClick(View.java:4793)
                                                                              at android.view.View$PerformClick.run(View.java:19960)
                                                                              at android.os.Handler.handleCallback(Handler.java:739)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:135)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5593)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

补充代码:

<1> 配置文件:

  <service android:name=".service.FirstService" >
<intent-filter>
<action android:name="m4" />
</intent-filter>
</service>

<2>

public class FirstService extends Service {
/*
* intent:startServcie(it)传入的值
* startId:启动服务的次数序号
*
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) { int x = intent.getIntExtra("x", 0);
//System.out.println("--"+startId+"--"+x);
if (x==88) {
//停止服务
stopSelf();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("--onDestroy");
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} }

<3>

public class MainActivity extends AppCompatActivity {

    TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
public void start(View v){
Intent it=new Intent();
it.setAction("m4");
it.putExtra("x", 88); startService(it);
}
}

错误修改:

 public void start(View v){
Intent it=new Intent();
it.setAction("m4");
it.putExtra("x", 88);
it.setPackage("myapplication.com.canvas");//应用的包名
startService(it);
}
上一篇:Base64加密工具-iBlogs


下一篇:wordpress的备份与还原