実際には、直接叩く(アクセス)するのではなくて、カスタムしたレシーバーを使って行う方法について書きます。以下のコードを試すときは、ServiceクラスをManifestファイルに追記することをお忘れなく…。
動作確認したコードをコピペしたので、問題なく動作するはずです。
ポイントとしては…
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
Button btn = new Button(this);
btn.setText("start");
ll.addView(btn);
setContentView(ll);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent service = new Intent(getApplicationContext(), TestService.class);
startService(service);
}
});
// receiver
UpdateReceiver receiver = new UpdateReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("DO_ACTION");
registerReceiver(receiver, filter);
}
protected class UpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();
String msg = extras.getString("message");
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}
}
NullableはAndroidStudioが自動で入れたものなので、気にしないでください。
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
public class TestService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId){
sendMessage("こんにちは");
stopSelf();
return START_NOT_STICKY;
}
protected void sendMessage(String msg){
Intent broadcast = new Intent();
broadcast.putExtra("message", msg);
broadcast.setAction("DO_ACTION");
getBaseContext().sendBroadcast(broadcast);
}
}