自定义android Intent Action与承袭TextView形成一个动态改变text的View

自定义android Intent Action与继承TextView形成一个动态改变text的View
//Activity
package com.android.Somejavaknowledget;

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Somejavaknowledget extends Activity {
/** Called when the activity is first created. */
public static final String MyAction="Customized-Action-ChangeText";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);*/
//客制化Title
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_gallery_title);
TextView tv = (TextView)getWindow().findViewById(R.id.left_text);
tv.setText("hello");


DateView dv= (DateView)findViewById(R.id.tv);
dv.setUpdates(true);
Button btn = (Button) getWindow().findViewById(
R.id.close);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("finish this activity");
finish();

}
});
Button show = (Button)findViewById(R.id.change);
show.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//自定义的Action
Intent intent= new Intent();
intent.setAction(MyAction);
sendBroadcast(intent);


}
});



}
}


//DateView 继承TextView 通过reciver broadcast 动态改变text

package com.android.Somejavaknowledget;



import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import android.view.MotionEvent;

import java.text.DateFormat;
import java.util.Date;
import java.util.Formatter;

public final class DateView extends TextView {
    private static final String TAG = "DateView";

    private boolean mUpdating = false;
    private Context mContext;
    private static int count=1;
  

    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
         /*   if (action.equals(Intent.ACTION_TIME_TICK)
                    || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
                updateClock();
            }*/
            if(action.equalsIgnoreCase(Somejavaknowledget.MyAction)){
            updateClock();
            }
        }

    };

    public DateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }
   
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        setUpdates(false);
    }

    @Override
    protected int getSuggestedMinimumWidth() {
        // makes the large background bitmap not force us to full width
        return 0;
    }

    private final void updateClock() {
        //Date now = new Date();
        //setText(DateFormat.getDateInstance(DateFormat.LONG).format(now));
    String s=String.format("count=%d", count);
    setText(s);
    count++;
    }

    void setUpdates(boolean update) {
        if (update != mUpdating) {
            mUpdating = update;
            if (update) {
                // Register for Intent broadcasts for the clock and battery
                IntentFilter filter = new IntentFilter();
                /*filter.addAction(Intent.ACTION_TIME_TICK);
                filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);*/
               filter.addAction(Somejavaknowledget.MyAction);
                mContext.registerReceiver(mIntentReceiver, filter, null, null);

                updateClock();
            } else {
                mContext.unregisterReceiver(mIntentReceiver);
            }
        }
    }
}