(7)Intents

What Is an Intent?

 Three types:activities, services, and broadcast receivers.

Intents can be passed to other applications written by other programmers, allowing them to be connected as modules of each other, if needed.

ACTION_DIAL Activity Displays thephone dialer

ACTION_BATTERY_LOW Broadcast ReceiverBattery low warning message

Implicit Intents & Explicit Intents

implicit intents is usually done via intent filters that are defined inthe AndroidManifest.xml file.

Intent filters are declared in AndroidManifest.xml using the <intent-filter> tag, and they filter based on three  attributes of the Intent object; action,data, and category.

If no action filters are specified, the action parameter of the intent will not be tested at all,moving the testing on to the data parameter of the intent. If no data filters are specified, then only intents that contain no data will be matched.

specifies that video MPEG4 and audio MPEG3 can be retrieved from the internet via HTTP:

<intent-filter>
<data android:mimeType="video/mp4" android:scheme="http" />
<data android:mimeType="audio/mp3" android:scheme="http" />
</intent-filter>

 (1)Using Intents with Activities


/layout/lock_analog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"> 
    <TextView 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="You Are Currently in: Activity #1"
		android:textColor="#fea"
		android:textSize="22dip"
		android:paddingBottom="20dip"/>
    <Button 
       android:tag="btn1"
       android:layout_width="200dip"
       android:layout_height="40dip"
       android:text="Go To Digital Clock: Activity #2"
       android:textSize="10dip"
       android:layout_gravity="center"/> 
     <AnalogClock 
         android:tag="analog1"
         android:layout_width="wrap_content"
		 android:layout_height="wrap_content"
		 android:layout_gravity="center"
		 android:layout_marginTop="30dip"
		 android:background="@drawable/image1" />
</LinearLayout>

lock_digital.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"> 
   <TextView 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="You Are Currently in: Activity #2"
		android:textColor="#fea"
		android:textSize="22dip"
		android:paddingBottom="20dip"/>
    <Button 
       android:tag="btn1"
       android:layout_width="200dip"
       android:layout_height="40dip"
       android:text="Go To Analog Clock: Activity #1"
       android:textSize="10dip"
       android:layout_gravity="center"/> 
    <DigitalClock 
        android:tag="digital1"
        android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center"
		android:layout_marginTop="30dip"
		android:textSize="32dip"
		android:textColor="#adf"
		android:typeface="monospace"/>
</LinearLayout>
public class LockAnalogActivity extends Activity {
	private View rootView;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		rootView=LayoutInflater.from(this).inflate(R.layout.lock_analog, null);
		this.setContentView(rootView);
		
		Button btn=(Button)rootView.findViewWithTag("btn1");
		btn.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(v.getContext(),LockDigitalActivity.class);
				startActivityForResult(intent,0);
			}
			
		});
	}
}
public class LockDigitalActivity extends Activity {
	private View rootView;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		rootView=LayoutInflater.from(this).inflate(R.layout.lock_digital, null);
		this.setContentView(rootView);
		Button btn=(Button)rootView.findViewWithTag("btn1");
		btn.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent replyIntent=new Intent();
				setResult(RESULT_OK,replyIntent);
				finish();//send the intent back
			}
			
		});
	}
}

(2)Using Intents with Services


A service needs to run asynchronously.A service can also be used by other Android applications, so it is more extensible than an activity. 

You need to subclass the Service class and implement at least its onCreate(), onStart(), and onDestroy() methods with your own custom programming logic. 

You also must declare the Service class in your AndroidManifest.xml file using the <service> tag.

/layout/lock_analog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"> 
    <TextView 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="You Are Currently in: Activity #1"
		android:textColor="#fea"
		android:textSize="22dip"
		android:paddingBottom="20dip"/>
    <Button 
       android:tag="btn1"
       android:layout_width="200dip"
       android:layout_height="40dip"
       android:text="Go To Digital Clock: Activity #2"
       android:textSize="10dip"
       android:layout_gravity="center"/> 
     <Button 
        android:tag="startBtn"
		android:text="Start the Media Player Service"
		android:textSize="10dip"
		android:layout_width="200dip"
		android:layout_height="40dip"
		android:layout_gravity="center"/>
	 <Button 
	    android:tag="stopBtn"
		android:text="Stop the Media Player Service"
		android:textSize="10dip"
		android:layout_width="200dip"
		android:layout_height="40dip"
		android:layout_gravity="center"/>
     <AnalogClock 
         android:tag="analog1"
         android:layout_width="wrap_content"
		 android:layout_height="wrap_content"
		 android:layout_gravity="center"
		 android:layout_marginTop="30dip"
		 android:background="@drawable/image1" />
</LinearLayout>

AndroidManifest.xml

	<service android:name=".MediaPlayerService" android:enabled="true"></service>		            
   </application>
public class MediaPlayerService extends Service {
	private MediaPlayer mPlayer;
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void onCreate(){
		mPlayer=MediaPlayer.create(this,R.raw.test);
		mPlayer.setLooping(true);
		
	}
	
	public void onStart(Intent intent,int startId){
		//trigger it with the start()
		mPlayer.start();
	}
	
	public void onDestroy(){
		//release memory containing the media player and the audio file
		mPlayer.stop();
	}

}
public class LockAnalogActivity extends Activity implements OnClickListener {
	private View rootView;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		rootView=LayoutInflater.from(this).inflate(R.layout.lock_analog, null);
		this.setContentView(rootView);
		
		Button btn=(Button)rootView.findViewWithTag("btn1");
		btn.setOnClickListener(this);
		
		Button btnStart=(Button)rootView.findViewWithTag("startBtn");
		btnStart.setOnClickListener(this);
		
		Button btnStop=(Button)rootView.findViewWithTag("stopBtn");
		btnStop.setOnClickListener(this);
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String tag=(String)v.getTag();
		if("btn1".equals(tag)){
			Intent intent=new Intent(v.getContext(),LockDigitalActivity.class);
			startActivityForResult(intent,0);
		}else if("startBtn".equals(tag)){
			startService(new Intent(getBaseContext(),MediaPlayerService.class));
		}else if("stopBtn".equals(tag)){
			stopService(new Intent(v.getContext(),MediaPlayerService.class));
		}
	}
}

(3)Using Intents with Broadcast Receivers


lock_digital.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"> 
   <TextView 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="You Are Currently in: Activity #2"
		android:textColor="#fea"
		android:textSize="22dip"
		android:paddingBottom="20dip"/>
    <Button 
       android:tag="btn1"
       android:layout_width="200dip"
       android:layout_height="40dip"
       android:text="Go To Analog Clock: Activity #1"
       android:textSize="10dip"
       android:layout_gravity="center"/> 
    <DigitalClock 
        android:tag="digital1"
        android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center"
		android:layout_marginTop="30dip"
		android:textSize="32dip"
		android:textColor="#adf"
		android:typeface="monospace"/>
    <EditText 
        android:tag="timeInSeconds"
        android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center"
		android:hint="Enter Number of Seconds Here!"
        android:inputType="numberDecimal"
		android:layout_marginTop="30dip"
		android:layout_marginBottom="30dip" />
	<Button 
	    android:tag="startTimer"
	    android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center"
		android:text="Start Timer Countdown" />
</LinearLayout>

AndroidManifest.xml

<receiver android:name=".TimerBroadcastReceiver" android:enabled="true" />

public class TimerBroadcastReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "Alarm Notification", Toast.LENGTH_LONG).show();
	}

}
public class LockDigitalActivity extends Activity {
	private View rootView;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		rootView=LayoutInflater.from(this).inflate(R.layout.lock_digital, null);
		this.setContentView(rootView);
		Button btn=(Button)rootView.findViewWithTag("btn1");
		btn.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent replyIntent=new Intent();
				setResult(RESULT_OK,replyIntent);
				finish();//send the intent back
			}
			
		});
		
		Button btnStartTimer=(Button)rootView.findViewWithTag("startTimer");
		btnStartTimer.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				timerAlert(v);
			}
			
		});
	}
	
	private void timerAlert(View v){
		EditText txtTime=(EditText)rootView.findViewWithTag("timeInSeconds");
		int i=Integer.parseInt(txtTime.getText().toString());
		
		Intent timerIntent=new Intent(this,TimerBroadcastReceiver.class);
		PendingIntent myPindingIntent=PendingIntent.getBroadcast(this.getApplicationContext(), 0, timerIntent, 0);
		
		AlarmManager myAlarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
		myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i*1000), myPindingIntent);
		Toast.makeText(this, "Alarm is set for "+i+" seconds", Toast.LENGTH_LONG).show();
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个是一个 JSON 格式的数组,要在 Python 中取值,可以使用 `json` 模块进行解析。示例代码如下: ```python import json # 假设你已经获取到了这个 JSON 格式的数组 messages = [ { "id": "1115162886977880124", "type": 0, "content": "Privileged Gateway Intents\\nSome Gateway Intents require approval if your bot is verified. If your bot is not verified, you can toggle those intents below to access them.\\n\\nPRESENCE INTENT\\nRequired for your bot to receive Presence Update events.\\n\\nNOTE: Once your bot reaches 100 or more servers, this will requi", "channel_id": "1114845986532110356", "author": { "id": "1113018315166728223", "username": "sks10086", "global_name": null, "avatar": null, "discriminator": "2655", "public_flags": 0, "avatar_decoration": null }, "attachments": [], "embeds": [], "mentions": [], "mention_roles": [], "pinned": false, "mention_everyone": false, "tts": false, "timestamp": "2023-06-05T06:19:10.980000+00:00", "edited_timestamp": null, "flags": 0, "components": [] }, { "id": "1115162806774403173", "type": 0, "content": "\\u6211\\u5df2\\u4e0a\\u7ebf\\uff01", "channel_id": "1114845986532110356", "author": { "id": "1114074530156130335", "username": "779959311qq.com", "global_name": null, "avatar": "1c16c57e526bdb691c4aba1fe604343f", "discriminator": "1523", "public_flags": 0, "bot": true, "avatar_decoration": null }, "attachments": [], "embeds": [], "mentions": [], "mention_roles": [], "pinned": false, "mention_everyone": false, "tts": false, "timestamp": "2023-06-05T06:18:51.858000+00:00", "edited_timestamp": null, "flags": 0, "components": [] } ] # 遍历 messages 数组中的每个元素,取出 content 字段的值 for message in messages: content = message['content'] print(content) ``` 在上面的代码中,我们遍历了 `messages` 数组中的每个元素,取出了 `content` 字段的值并打印出来。你可以根据自己的需求修改代码来获取其他字段的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值