80,351
社区成员
发帖
与我相关
我的任务
分享
package com.example.servicetest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Toast toast;
MyHandler mHandler =null;
Button btStart,btStop;
public static final String RECEIVER = ".MyService";
//private final String ACTION="com.example.servicetest";
//MyService my_service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btStart=(Button)this.findViewById(R.id.bt_start);
btStop=(Button)this.findViewById(R.id.bt_stop);
btStart.setOnClickListener(this);
btStop.setOnClickListener(this);
mHandler=new MyHandler(Looper.getMainLooper());
//SSMR=new SerSocketMsgRece(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void ToastTip(String context,boolean IsShort)
{
int duration;
if(IsShort==true)
{
duration=Toast.LENGTH_SHORT;
}else{
duration=Toast.LENGTH_LONG;
}
CharSequence textE="";
textE=context;
toast=Toast.makeText(this, textE, duration);
toast.show();
}
public class MyHandler extends Handler{
String nowStr=null;
public MyHandler(Looper looper){
super(looper);
}
@SuppressLint("NewApi")
@Override
public void handleMessage(Message msg) {//处理消息
switch(msg.what)
{
case 0:
{
ToastTip("服务已被启动",true);
}break;
default:break;
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.bt_stop:
{
Intent service=new Intent("com.example.myservice");
stopService(service);
}break;
case R.id.bt_start:{
Intent service=new Intent("com.example.myservice");
startService(service);
}break;
default:break;
}
}
public class SerSocketKeeper extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mHandler.sendEmptyMessage(0);
}
}
}
package com.example.servicetest;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
//StartService方式启动
public class MyService extends Service {
Handler handler;
private final String ACTION="com.example.servicetest";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.v("onBind","bind");
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.v("onCreate","Create");
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
SendMsgFlushUI_Board();
}
};
startThread();//启动线程
}
private void startThread() {
new Thread(){
public void run(){
Intent intent = new Intent();
int i = 0;
while(true){
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
i++;
handler.sendEmptyMessage(0);
}
}
}.start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v("onDestroy","destriy");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.v("onStartCommand","StartCommand");
return super.onStartCommand(intent, flags, startId);
}
public void SendMsgFlushUI_Board()
{
Intent boradcastIntent = new Intent(ACTION);
sendBroadcast(boradcastIntent);//这里
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.servicetest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService">
<intent-filter>
<action android:name="com.example.myservice" />
</intent-filter>
</service>
<!-- 注册广播接收 -->
<receiver android:name=".SerSocketKeeper">
<intent-filter android:priority="0">
<action android:name="com.example.servicetest"/>
</intent-filter>
</receiver>
</application>
</manifest>
<!-- 注册广播接收 -->
<receiver android:name="com.example.servicetest.MainActivity$SerSocketKeeper">
<intent-filter android:priority="0">
<action android:name="com.example.servicetest"/>
</intent-filter>
</receiver>
这样注册的话SerSocketKeeper要定义成静态类,静态类是没有MainActivity的context的,所以不能使用mHandler,除非mHandler也定义成静态的。
楼主需要在代码里注册广播接收器,代码帮你改好了:
package com.example.servicetest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsMessage;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Toast toast;
MyHandler mHandler =null;
Button btStart,btStop;
public static final String RECEIVER = ".MyService";
private final String ACTION="com.example.servicetest";
private SerSocketKeeper ser = new SerSocketKeeper();//初始化
//MyService my_service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btStart=(Button)this.findViewById(R.id.bt_start);
btStop=(Button)this.findViewById(R.id.bt_stop);
btStart.setOnClickListener(this);
btStop.setOnClickListener(this);
mHandler=new MyHandler(Looper.getMainLooper());
//SSMR=new SerSocketMsgRece(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void ToastTip(String context,boolean IsShort)
{
int duration;
if(IsShort==true)
{
duration=Toast.LENGTH_SHORT;
}else{
duration=Toast.LENGTH_LONG;
}
CharSequence textE="";
textE=context;
toast=Toast.makeText(this, textE, duration);
toast.show();
}
public class MyHandler extends Handler{
String nowStr=null;
public MyHandler(Looper looper){
super(looper);
}
@SuppressLint("NewApi")
@Override
public void handleMessage(Message msg) {//处理消息
switch(msg.what)
{
case 0:
{
ToastTip("服务已被启动",true);
}break;
default:break;
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.bt_stop:
{
Intent service=new Intent("com.example.myservice");
stopService(service);
unregisterReceiver(ser);//注销广播接收器
}break;
case R.id.bt_start:{
Intent service=new Intent("com.example.myservice");
startService(service);
//注册广播接收器
IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
filter.addAction(ACTION);
registerReceiver(ser, filter);// 注册Broadcast Receiver
}break;
default:break;
}
}
public class SerSocketKeeper extends BroadcastReceiver
{
//加上一个默认的构造器
public SerSocketKeeper() {
// TODO Auto-generated constructor stub
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mHandler.sendEmptyMessage(0);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.servicetest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService">
<intent-filter>
<action android:name="com.example.myservice" />
</intent-filter>
</service>
<!-- 这一段不要了,放在代码里注册-->
<!-- 注册广播接收 -->
<!-- receiver android:name=".SerSocketKeeper">
<intent-filter android:priority="0">
<action android:name="com.example.servicetest"/>
</intent-filter>
</receiver>
-->
</application>
</manifest>