На AVD 5.0+ валится с ошибкой. Попробовал запустить на 4.4 - приложение уже не вылает, но выдает "Implicit intents with startService are not safe: Intent { act=ru.startandroid.develop.p0942servicekillserver.MyService (has extras) } android.content.ContextWrapper.startService:494 com.example.pavel.ololosh.MainActivity.onClickStart:19 java.lang.reflect.Method.invokeNative:-2". На AVD 4.0.3 - вообще при клике на кнопку ничего не происходит.
Работаю из под Android Studio. В уроке сказано первым установить на AVD сервис, но вместо установки он у меня просто запускается и проганяется. А из другого application не запускается. Ниже код:
MyService:
Код: Выделить всё
public class MyService extends Service {
final String LOG_TAG = "myLogs";
public void onCreate() {
super.onCreate();
Log.d(LOG_TAG, "MyService onCreate");
}
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "MyService onDestroy");
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(LOG_TAG, "MyService onStartCommand");
readFlags(flags);
MyRun mr = new MyRun(startId);
new Thread(mr).start();
return START_NOT_STICKY;
}
public IBinder onBind(Intent arg0) {
return null;
}
void readFlags(int flags) {
if ((flags&START_FLAG_REDELIVERY) == START_FLAG_REDELIVERY)
Log.d(LOG_TAG, "START_FLAG_REDELIVERY");
if ((flags&START_FLAG_RETRY) == START_FLAG_RETRY)
Log.d(LOG_TAG, "START_FLAG_RETRY");
}
class MyRun implements Runnable {
int startId;
public MyRun(int startId) {
this.startId = startId;
Log.d(LOG_TAG, "MyRun#" + startId + " create");
}
public void run() {
Log.d(LOG_TAG, "MyRun#" + startId + " start");
try {
TimeUnit.SECONDS.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
stop();
}
void stop() {
Log.d(LOG_TAG, "MyRun#" + startId + " end, stopSelfResult("
+ startId + ") = " + stopSelfResult(startId));
}
}
}
Код: Выделить всё
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pavel.servicekillserver" >
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" >
<service android:name=".MyService">
<intent-filter>
<action android:name="ru.startandroid.develop.p0942servicekillserver.MyService"/>
</intent-filter>
</service>
</application>
</manifest>
Код: Выделить всё
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickStart(View v) {
Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();
startService(new Intent("ru.startandroid.develop.p0942servicekillserver.MyService").putExtra("name", "value"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}