1、新建一个activity
2、在想要跳转到别的界面的界面编写跳转代码
private Button button1, button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button01);
button2 = (Button) findViewById(R.id.button02);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MessageActivity.class); //创建一个意图,跳转到message activity
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); //跳转到系统网络设置
startActivity(intent);
}
});
}
1、创建界面
2、在当前界面编写跳转代码
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MessageActivity extends ActionBarActivity {
private EditText edit1;
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
edit1 = (EditText) findViewById(R.id.edit01);
button1 = (Button) findViewById(R.id.button01);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MessageActivity.this, ShowActivity.class);
String result = edit1.getText().toString();
intent.putExtra("message", result); //传输数据
startActivity(intent);
}
});
}
@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_message, 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);
}
}
3、在接收界面编写接收代码
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class ShowActivity extends ActionBarActivity {
private TextView text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
text2 = (TextView) findViewById(R.id.text02);
Intent intent = getIntent(); //获取当前的意图
Bundle bundle = intent.getExtras(); //定义接收数据的对象
if (bundle != null) {
String message = bundle.getString("message"); //接收数据,什么数据就get什么
text2.setText(message);
}
}
@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_show, 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);
}
}
创建一个service
(1)mainactivity编写调用服务的代码
private Button button1, button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button01);
button2 = (Button) findViewById(R.id.button02);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == button1.getId()) {
Toast.makeText(MainActivity.this, "开始播放背景音乐!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, MusicService.class); //创建一个意图
startService(intent);
}
else if (v.getId() == button2.getId()) {
Toast.makeText(MainActivity.this, "停止播放背景音乐!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, MusicService.class);
stopService(intent);
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
}
(2)service代码
public class MusicService extends Service {
private MediaPlayer player;
public MusicService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(MusicService.this, R.raw.sample);
}
}
启动时,先进onCreate和onBind方法,当activity销毁的时候service也会随之销毁
(1)界面启动service代码
public class MainActivity extends ActionBarActivity {
private Button button1;
private TextView tv2;
private ICountService countService;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
countService = (ICountService) service;
tv2.setText("Count is " + countService.getCount());
}
@Override
public void onServiceDisconnected(ComponentName name) {
countService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button01);
tv2 = (TextView) findViewById(R.id.text02);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv2.setText("Count is " + countService.getCount());
}
});
Intent intent = new Intent(MainActivity.this, CountService.class);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
@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
protected void onDestroy() {
unbindService(serviceConnection);
super.onDestroy();
}
(2)countservice
public class CountService extends Service {
private boolean threadDisable;
private int count;
private ServiceBinder serviceBinder = new ServiceBinder();
public class ServiceBinder extends Binder implements ICountService {
public int getCount() {
return count;
}
}
public CountService() {
}
@Override
public IBinder onBind(Intent intent) {
return serviceBinder;
}
@Override
public void onCreate() {
super.onCreate();
new Thread(new Runnable() {
@Override
public void run() {
while (!threadDisable) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
count++;
}
}
}).start();
}
@Override
public void onDestroy() {
super.onDestroy();
threadDisable = true;
}
}
(3)icountservice
public interface ICountService {
public abstract int getCount();
}