我正在使用Handler
从主线程接收消息。但是当主线程调用方法(threadmsg)时,Handler
为NULL但已创建Handler
。
我的代码:
package com.example.smsnotify;
import java.io.PrintWriter;
import java.net.Socket;
import com.androidexample.broadcastreceiver.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final String TAG = "Debug";
Looper mLooper;
Handler handler=null;
Message msg;
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public void threadMsg(String msg) {
Log.i(TAG,"Before sending msg1");
if (!msg.equals(null) && !msg.equals("")) {
if(handler==null)
{
Log.i(TAG,"NULL handler");
}
Message msgObj = handler.obtainMessage();
Log.i(TAG,"Before sending msg2");
Bundle b = new Bundle();
b.putString("message", msg);
msgObj.setData(b);
Log.i(TAG,"Before sending msg3");
handler.sendMessage(msgObj);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread background = new Thread(new Runnable() {
// After call for background.start this run method call
public void run() {
try {
//threadMsg(SetServerString);
Looper.prepare();
Log.i(TAG,"New handler created");
handler = new Handler() {
public void handleMessage(Message msg) {
Socket client;
PrintWriter printwriter;
String aResponse = msg.getData().getString("message");
Log.i(TAG,"Got new msg:"+aResponse);
}
};
} catch (Throwable t) {
// just end the background thread
Log.i("Debug", "Thread exception " + t);
}
Looper.loop();
}
});
background.start();
finish();
//image = (ImageView)findViewById(R.id.imageView1);
//image.setImageResource(R.drawable.globe);
}
/** Called when the user clicks the Send button */
}
收到短信后,我从其他Android代码调用threadmsg函数。在那种情况下我得到“处理程序NULL”登录logcat。这段代码有什么问题?有谁可以帮助我?
其他Android代码我正在调用此函数:
package com.example.smsnotify;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.content.*;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.telephony.*;
import android.util.Log;
import android.widget.Toast;
public class IncomingSms extends BroadcastReceiver{
private static final String TAG = "Debug";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG,"onreceive called");
// Create Inner Thread Class
MainActivity mac=new MainActivity();
Log.i(TAG,"got sms");
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i(TAG, messages.getMessageBody());
Toast.makeText(context, "SMS Received : "+messages.getMessageBody(),
Toast.LENGTH_LONG).show();
mac.threadMsg(messages.getMessageBody());
//msg = mHandler.obtainMessage();
//mHandler.sendEmptyMessage(2);
// isms.inform_middleware();
}
}