//在 SetContentView 前加上下面代码 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
package com.example.androidtest; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { Timer timer1 = new Timer(); int i = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { //如果本消息是该程序发送的,则执行下面步骤 if(msg.what == 0x1233) { /* * 这里可以编写改变界面布局的代码, * 由下面的Timer 定时间动态发送 msg.what = 0x1233; * 发送完则调用handleMessage函数 * handlerMessage 识别到 0x1233,说明是本程序发送的消息 * 则执行下面步骤 */ Log.v("test", " test:" + (i++)); } } }; timer1.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub Message msg = new Message(); //定义识别标志0x1233 msg.what = 0x1233; myHandler.sendMessage(msg); } }, 0, 800); } @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; } }
//声明对象 SharedPreferences preferences; SharedPreferences.Editor editor; preferences = getSharedPreferences("example",MODE_PRIVATE); editor = preferences.edit(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日"+"hh:mm:ss"); //存数据 editor.putString("time", sdf.format(new Date())); editor.putInt("random", (int)(Math.random()*100)); editor.commit(); //取数据 String time = preferences.getString("time", null); int randNum = preferences.getInt("random", 0);
package com.example.androidtest; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { SharedPreferences preferences; SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * MODE_PRIVATE表示被该应用程序读 * MODE_WORLD_READABLE 表示数据可以被其他应用程序读,但不能被其他应用程序写 * MODE_WORLD_WRITEABLE 表示数据可以被其他应用程序写 */ preferences = getSharedPreferences("example",MODE_PRIVATE); editor = preferences.edit(); Button read = (Button)findViewById(R.id.read); Button write = (Button)findViewById(R.id.write); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //取数据 String time = preferences.getString("time", null); int randNum = preferences.getInt("random", 0); String result = time == null ? "您还没存入数据!":"写入时间为"+time+"\n 上次生成的随机数为:"+randNum; Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show(); } }); write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //格式化数据 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日"+"hh:mm:ss"); //存数据 editor.putString("time", sdf.format(new Date())); editor.putInt("random", (int)(Math.random()*100)); editor.commit(); } }); } @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; } }
//发送数据的Activity //新建一个Bundle类 Bundle bundle = new Bundle(); //Bundle类中加入数据(key- value的方式,另一个activity获取数据时需要key) bundle.putString("first", "第一个BroadCastReceive 存入的消息"); //新建一个intent对象,并将该bundle加入这个intent对象 Intent intent = new Intent(); intent.putExtras(bundle); intent.setClass(FirstActivity.this,SecondActivity.class); startAtivity(intent); //接收数据的Activity Bundle bundle = getIntent().getExtras(); //读出数据 String data = bundle.getString("first");
<receiver android:name=".MyReceiver"> <intent-filter > <action android:name="llp"/> </intent-filter> </receiver>
package com.example.androidtest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button send = (Button)findViewById(R.id.read); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); //这里要跟AndroidMainfest里面的 action 的 name 一样 //action name 最好包含包名 intent.setAction("llp"); intent.putExtra("msg", "simple message"); sendBroadcast(intent); } }); } }
package com.example.androidtest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "接收到的Intent的Action为:"+intent.getAction()+"\n消息内容为:"+intent.getStringExtra("msg"), 5000).show(); } }
//优先接收者将将数据存入 BroadCast 中 Bundle bundle = new Bundle(); bundle.putString("first", "第一个BroadCastReceive 存入的消息"); setResultExtras(bundle); //终止BroadCast Intent 的传播 // abortBroadcast(); //下一个接受者获取数据 Bundle bundle = getResultExtras(true); String first = bundle.getString("first");
<receiver android:name=".MyReceiver"> <!-- 注明优先级为20 --> <intent-filter android:priority="20" > <action android:name="llp"/> </intent-filter> </receiver> <receiver android:name=".MyReceiver2"> <!-- 注明优先级为0 ,范围为:-1000:1000 --> <intent-filter android:priority="0"> <action android:name="llp"/> </intent-filter> </receiver>
//发送广播代码 package com.example.androidtest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button send = (Button)findViewById(R.id.read); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("llp"); intent.putExtra("msg", "simple message"); //发送有序广播 sendOrderedBroadcast(intent, null); } }); } } //高优先级接受者 package com.example.androidtest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "接收到的Intent的Action为:"+intent.getAction()+"\n消息内容为:"+intent.getStringExtra("msg"), 5000).show(); Bundle bundle = new Bundle(); bundle.putString("first", "第一个 BroadCastReceive 存入的消息"); //处理消息并传递给下一个接收者 setResultExtras(bundle); abortBroadcast(); } } //低优先级接受者 package com.example.androidtest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class MyReceiver2 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //获取数据 Bundle bundle = getResultExtras(true); String first = bundle.getString("first"); Toast.makeText(context, "第一个BroadCastRecver 存入的消息为:"+first, 5000).show(); } }