Android常用方法
发广播
在Receiver类中, 发送一条带着名称为name,值为value的广播,广播名com.lxh.test.action.nameTAG。
Intent intent_gb = new Intent();
intent_gb.setAction("com.lxh.test.action.nameTAG");//广播名
intent_gb.putExtra("name", "value");
context.sendBroadcast(intent_gb);
开页面
在 Receiver 类中开启新的activity
Intent intentx = new Intent(context, MainActivity.class);//页面名
intentx.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentx);
在activity中开启新的activity
开服务
在 Receiver 类中开启新的服务
Intent it1 = new Intent("com.android.smart.Service");//服务名
it1.setPackage("com.lxh.Service");//服务包名
//if (!(context instanceof Activity)) {
//it1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//}//不知还有没有效果
context.startService(it1);
在activity中开启新的服务
Intent it1 = new Intent("com.android.smart.Service");
it1.setPackage("com.lxh.Service");
startService(it1);
mIntent = new Intent(SocketActivity.this, TCPServerService.class);
startService(mIntent);
activity生命周期
package com.my.nl.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import com.my.nl.R;
public class NetChooseActivity extends AppCompatActivity {
private static final String TAG = "NetChooseActivity";
Context mcontext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net_choose);
mcontext = NetChooseActivity.this;
Log.i(TAG, "lxh onDestroy->onCreate->onStart");
}
@Override
protected void onStart() {
Log.i(TAG, "lxh onCreate/onRestart->onStart->onResume");
super.onStart();
}
@Override
protected void onResume() {
Log.i(TAG, "lxh onStart->onResume<->onPause");
super.onResume();
}
@Override
protected void onPause() {
Log.i(TAG, "lxh onResume<->onPause->onStop/onCreate");
super.onPause();
}
@Override
protected void onStop() {
Log.i(TAG, "lxh onPause->onStop->onDestroy/onRestart");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(TAG, "lxh onStop->onDestroy->onCreate");
super.onDestroy();
}
@Override
protected void onRestart() {
Log.i(TAG, "lxh onStop->onRestart->onStart");
super.onRestart();
}
}
package com.my.nl.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import com.my.nl.R;
public class NetChooseActivity extends AppCompatActivity {
private static final String TAG = "NetChooseActivity";
Context mcontext;
@Override
protected void onStart() {
Log.i(TAG, "lxh onStart");
super.onStart();
}
@Override
protected void onResume() {
Log.i(TAG, "lxh onResume");
super.onResume();
}
@Override
protected void onPause() {
Log.i(TAG, "lxh onPause");
super.onPause();
}
@Override
protected void onStop() {
Log.i(TAG, "lxh onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(TAG, "lxh onDestroy");
super.onDestroy();
}
@Override
protected void onRestart() {
Log.i(TAG, "lxh onRestart");
super.onRestart();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net_choose);
mcontext = NetChooseActivity.this;
Log.i(TAG, "lxh onCreate");
}
}