试试这个:
button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Create a piece of toast.
Toast pieceToast = Toast.makeText(getApplicationContext(), "Test Message", Toast.LENGTH_SHORT);
// Show the toast.
pieceToast.show();
}
这是我的 BroadcastReciever 类:
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("IncomingCallReceiver",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
String registredPhoneNumber;
Log.i("IncomingCallReceiver","State: "+ state);
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("*****Mobile Ringing*******"+phonenumber);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
registredPhoneNumber = PreferenceConnector.getPhoneNumber(context);
System.out.println("registredPhoneNumber: "+registredPhoneNumber);
System.out.println("phonenumber: "+phonenumber);
Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);
if(phonenumber.equals(registredPhoneNumber)){
System.out.println("Entered...");
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
if(isMyServiceRunning(context)){
context.stopService(new Intent(context,com.visiomaticamericas.visitormobile.services.LaunchServiceActivity.class));
System.out.println("******Service Stopped*********");
}
Intent i = new Intent(context,com.services.LaunchServiceActivity.class);
i.putExtra("delay",500L);
context.startService(i);
System.out.println("*****Service Started*****");
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
}
}
在这里,我调用了我的活动类..并在我的活动类中进行了小部件设计..