Android:为什么我的服务没有启动?

时间:2016-12-23 12:16:03

标签: java android android-studio service

我想学习如何在我的应用程序中使用服务,但服务不起作用!这是我的代码。 这是我为服务编写的示例代码。建议我,如果我遗漏任何东西。谢谢你提前

package com.example.utente.test;

import android.content.Intent
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemClickListener {
final String[] numeri =
{"1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"
,"1","2","1","2",
"1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2",
"1","2","1","2"};
long inizio;
long fine;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inizio = System.currentTimeMillis();
Intent i= new Intent(getApplicationContext(), MyService.class);
startService(i);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.riga, numeri);
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onResume()
{
super.onResume();
inizio = System.currentTimeMillis();
//     new MyService(inizio);
}
@Override
public void onItemClick(AdapterView<?> adattatore, final View componente,
int posizione, long id )
{
Toast.makeText(getApplicationContext(), numeri[posizione],
Toast.LENGTH_LONG).show();
}
}
package com.example.utente.test;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service
{
long inizio;
long fine;
long tempo;
boolean ciclo=true;
int prova=0;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// This is where you would place your code that you want in the
background
// Putting your while loop here will make sure it runs when the app is closed
Toast.makeText(getApplicationContext() ,"COMMAND",
Toast.LENGTH_LONG).show();
while(ciclo)
{
fine = System.currentTimeMillis();
tempo = (fine-inizio)/1000;
if(tempo>10 && prova==0)
{
prova++;
Toast.makeText(getApplicationContext(), "10s",
Toast.LENGTH_LONG).show();
}
}
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
}
<?xml version="1.0" encoding="utf-8"?>

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.utente.test">
<service
android:name="MyService" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

1 个答案:

答案 0 :(得分:0)

Service添加到Manifest,如下所示:

<service android:name=".MyService" />