服务不工作

时间:2014-09-26 14:05:54

标签: android service notifications

我需要在OnStartCommand方法中显示Toast并在logCat中显示OnCreate方法。 这是主要的课程:

public class MainActivity extends ActionBarActivity {


WebView webView;
ProgressBar progressBar;
String url = "http://www.amrp.tecnogeppetto.it/";
final Intent Service = new Intent(getBaseContext(),Services.class);


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

    webView = (WebView) findViewById(R.id.webView1);    
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    //new LoadUrl().execute(url);
    startService(Service);

    }

}

这是Services.java类:

    import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class Services extends Service {
    public static final String TAG = "Services";
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    public void onCreate(){
         Log.d(TAG,"onCreate");
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"onStartCommand");
        Toast.makeText(this, "ooo", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }
}

我还在Manifest中将服务视为:

    ....
    <service android:name=".Services" />
    </application>

</manifest>

帮帮我。感谢

编辑:错误日志

    09-26 14:34:34.059: E/AndroidRuntime(1472): FATAL EXCEPTION: main
09-26 14:34:34.059: E/AndroidRuntime(1472): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.amrp/com.example.amrp.MainActivity}: java.lang.NullPointerException
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.os.Looper.loop(Looper.java:137)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.app.ActivityThread.main(ActivityThread.java:4340)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at java.lang.reflect.Method.invoke(Method.java:511)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at dalvik.system.NativeStart.main(Native Method)
09-26 14:34:34.059: E/AndroidRuntime(1472): Caused by: java.lang.NullPointerException
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.content.ComponentName.<init>(ComponentName.java:75)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.content.Intent.<init>(Intent.java:3004)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at com.example.amrp.MainActivity.<init>(MainActivity.java:24)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at java.lang.Class.newInstanceImpl(Native Method)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at java.lang.Class.newInstance(Class.java:1319)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
09-26 14:34:34.059: E/AndroidRuntime(1472):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
09-26 14:34:34.059: E/AndroidRuntime(1472):     ... 11 more

5 个答案:

答案 0 :(得分:0)

在保存方提供清单

<service android:name="android.app.Service.Services" />

尝试这样的事情......

 (new Timer()).schedule(
     new TimerTask() {
        public void run() {
           handler.post(new Runnable() {
              public void run() {
                 Toast.makeText(getApplicationContext(), "ooo", Toast.LENGTH_LONG).show();
              }
           });
        }
     }, ur sleep Time);

     handler = new Handler();
     // onHandleIntent ...
handler.post(new Runnable() {
   @Override
   public void run() {
      Toast.makeText(getApplicationContext(), "ooo", Toast.LENGTH_SHORT).show();
   }
});

答案 1 :(得分:0)

使用以下命令更改MainActivity类代码:

public class MainActivity extends ActionBarActivity {

    WebView webView;
    ProgressBar progressBar;
    String url = "http://www.amrp.tecnogeppetto.it/";

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

        webView = (WebView) findViewById(R.id.webView1);    
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        //new LoadUrl().execute(url);
        Intent intent = new Intent(MainActivity.this, Services.class)
        startService(intent);
    }

}

答案 2 :(得分:0)

我认为您的问题是您创建服务Intent的方式。尝试使用对主Activity的引用,它将显示Toast。并且不要在服务变量名称中使用大写

Intent service = new Intent(this, Services.class);
startService(service);

答案 3 :(得分:0)

您需要在MainActivity课程中删除这些声明:

final Intent Service = new Intent(getBaseContext(), Services.class);
startService(Service);

并将其替换为:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Other codes here.
    Intent service = new Intent(this, Services.class);
    startService(service);
}

Services课程中,请执行以下操作:

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG,"onStartCommand");
    Toast.makeText(getApplicationContext(), "ooo", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

请勿使用getBaseContext()

答案 4 :(得分:0)

问题出在Toast.makeText(this, "ooo", Toast.LENGTH_LONG).show();

服务不是UI线程。 你不能在没有UI线程的情况下使用Toast。