Android第三天
来源:互联网 发布:怎么修改淘宝付款密码 编辑:程序博客网 时间:2024/06/05 15:38
一.Intent两种启动方式(以启动Activity为例)
1.显示启动:
//指定当前的应用程序上下文及要启动的ActivityIntent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
2.隐式启动:不直接指定需要启动哪个Activity或者调用哪个Activity,而是设置一个筛选条件,满足条件才启动
//java 文件中:Intent intent = new Intent("dog");
startActivity(intent);
//AndroidMainfest.xml文件中:
<receiver android:name="com.example.broadcastreceive.BroadCastReceive" >
<intent-filter >
<action android:name="dog"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
二.BroadcastReceive组件
1.添加一个Button发送广播消息,设置隐式Intent筛选条件,满足过滤条件intent-filter的才能收到广播消息.
MainActivity.java:
package com.example.broadcastreceive;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button button = (Button) findViewById(R.id.broad);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//隐式Intent
Intent intent = new Intent("hello");
//使用putExtra()方法将要发送的信息捆绑到Intent上
intent.putExtra("key", "go to moon...");
//发送广播
sendBroadcast(intent);
}
} );
}
}
BroadCastReceive.java:
package com.example.broadcastreceive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadCastReceive extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//将接收到的广播消息显示在LogCat上
Log.v("1 hello",arg1.getStringExtra("key"));
}
}
BroadCastReceive2.java:
package com.example.broadcastreceive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadCastReceive2 extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//将接收到的广播消息显示在LogCat上
Log.v("2 hello",arg1.getStringExtra("key"));
}
}
AndroidMainfest.xml文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceive"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcastreceive.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>
<receiver android:name="com.example.broadcastreceive.BroadCastReceive" >
<intent-filter >
<action android:name="hello"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<receiver android:name="com.example.broadcastreceive.BroadCastReceive2" >
<intent-filter >
<action android:name="hello"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>
2. 添加两个Button,一个Button去启动一个服务,服务开始时启动一个线程计时, 另一个Button去停止线程,但不能停止服务,线程停止后还可以点击Button继续计时,时间紧接上次停止时的时间,同是计时的时间也
要在Activity上显示出来,用户可见的.
MainActivity.java:
package com.example.broadcastreceive3;import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
public Mycast1 mycast1;
public Mycast2 mycast2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
// 发送广播功能:
// 发送特定广播,启动一个服务,内有一只计时器
Button button1 = (Button) findViewById(R.id.start);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent("97.7MHZ");
intent.putExtra("key", "来自97.7MHZ的广播...");
sendBroadcast(intent);
}
});
// 停止线程按钮功能:
// 再发送一个广播,停止服务里的计时线程
Button button2 = (Button) findViewById(R.id.stop);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent("st");
sendBroadcast(intent);
}
});
}
//动态注册广播信息,不用在AndroidMainfest.xml文件中去注册
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// 构建广播接收器对象
mycast1 = new Mycast1();
// 构建广播接受的的过滤器
IntentFilter filter1 = new IntentFilter();
filter1.addAction("97.7MHZ");
// 将接收器和过滤器绑定
registerReceiver(mycast1, filter1);
mycast2 = new Mycast2();
// 构建广播接受的的过滤器
IntentFilter filter2 = new IntentFilter();
filter2.addAction("T");
// 将接收器和过滤器绑定
registerReceiver(mycast2, filter2);
}
// 创建广播接收器1
class Mycast1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v("Tag", intent.getStringExtra("key"));
intent = new Intent(MainActivity.this, Server.class);
startService(intent);
}
}
// 创建广播接收器2
class Mycast2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int count = 0;
//接收Intent负载信息
count = intent.getIntExtra("T", count);
//设置TextView文本
TextView text1 = (TextView)findViewById(R.id.text);
text1.setText("开始计时: "+count);
}
}
//Activity结束时解除广播注册信息
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(mycast1);
unregisterReceiver(mycast2);
}
}
Server.java:
package com.example.broadcastreceive3;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
public class Server extends Service {
public boolean flag;
public Mythread thread;
public static int count;
public threadcast broadcast;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(broadcast);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
flag = false;
// 启动计时线程
thread = new Mythread();
thread.start();
// 构建广播接收器对象
broadcast = new threadcast();
// 设置过滤器条件
IntentFilter filter = new IntentFilter("st");
// 绑定接收器和过滤器到一块
registerReceiver(broadcast, filter);
}
// 创建计时线程
class Mythread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (!flag) {
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
Log.v("Tag", "计时" + (count++) + "秒");
Intent intent = new Intent("T");
intent.putExtra("T", count);
sendBroadcast(intent);
}
}
public void st() {
flag = true;
}
}
// 创建广播接收器
class threadcast extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
// 关闭线程
thread.st();
}
}
}
最后在AndroidMainfest.xml文件中注册一下Server服务:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceive3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcastreceive3.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="com.example.broadcastreceive3.Server">
</service>
</application>
</manifest>
三.调试问题
1.Warning: Activity not started, its current task has been brought to the front
修改程序以后未编译,模拟器里有之前的APK安装包,没有卸载和重新安装.将模拟器关掉,重新编译运行一下即可.
2. 今天手抖删除了appcompat_v7_7的包,结果src和res文件夹下出现大量红叉叉,点进去一个看错误提示:
error: Error retrieving parent for item: No resource found that matches the given name‘Theme.AppCompat.Light’.
解决方法:
将sdk >extra>android>support>v7下的appcompat导入到eclipse中,
右键你有错误的项目,点击最下边的 properties,进去之后选择左侧列表中的Android,再把那个大红叉
那个给Remove,点击Add,添加刚导入的appcompat,保存退出即可.
阅读全文
0 0
- android第三天总结
- 第三天 android体系结构
- Android基础第三天
- Android第三天
- android自学第三天
- Android基础第三天
- android第三天
- Android开发第三天
- Android 第三天布局
- Android集训第三天
- Android第三天
- Android第三天
- Android第三天
- Android学习笔记: 第三天
- worklight第三天,集成android
- Android自学笔记 第三天
- Android 第三天(上午)
- Android 第三天(下午)
- 图像放缩中最近邻插值和双线性插值的基本原理
- synchronized VS Lock, wait-notify VS Condition
- Hibernate入门配置案例
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- LeetCode题目分类
- Android第三天
- C++之成员函数内存布局③---补充(13)《Effective C++》
- Elasticsearch检索分类深入详解—基础篇
- HDU1213 How Many Tables 并查集入门
- 导航 4
- 有名信号量实现读者-写着问题(写者优先)
- 基于aiohttp web框架的微信公众号开发(项目)
- Leetcode--338. Counting Bits
- git的push与fetch