赞
踩
构建新的xml
package cn.itcast.codeui; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); TextView textView = new TextView(this); textView.setText(R.string.hello); ViewGroup.LayoutParams textViewParams = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); linearLayout.addView(textView, textViewParams); View partView = getPartUI(); //该部分是固定的 上面是动态的 linearLayout.addView(partView); ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); setContentView(linearLayout, layoutParams); } private View getPartUI(){ LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); return inflater.inflate(R.layout.part, null); //由界面文件生成一个view对象,要不要挂一个父类元素 } }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
-
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="添加"
- />
- </LinearLayout>

- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, MainActivity!</string>
- <string name="app_name">发送通知</string>
- <string name="shorttitle">概要</string>
- <string name="title">标题</string>
- <string name="content">内容</string>
- <string name="button">发送</string>
- </resources>
layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/shorttitle"
- />
-
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/shorttitle"
- />
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/title"
- />
-
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/title"
- />
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/content"
- />
-
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/content"
- />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button"
- android:onClick="send"
- />
- </LinearLayout>

MainActivity.java
- package cn.itcast.notification;
-
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
-
- public class MainActivity extends Activity {
- private EditText shorttitleText;
- private EditText titleText;
- private EditText contentText;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- shorttitleText = (EditText) this.findViewById(R.id.shorttitle);
- titleText = (EditText) this.findViewById(R.id.title);
- contentText = (EditText) this.findViewById(R.id.content);
- }
-
- public void send(View v){
- String tickerText = shorttitleText.getText().toString();
- String title = titleText.getText().toString();
- String content = contentText.getText().toString();
- int icon = android.R.drawable.stat_notify_chat;
- Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
- Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:194949494"));
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 10, intent, 0);
- notification.setLatestEventInfo(this, title, content, pendingIntent); // pengingIntent 指点击通知时候打开那个应用
- notification.defaults = Notification.DEFAULT_SOUND;//声音
- notification.flags = Notification.FLAG_AUTO_CANCEL; //通知被点击则取消显示
-
- NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- manager.notify(100, notification);//100是id 自定义唯一
- }
- }

- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.itcast.notification"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".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>
-
- </application>
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="android.permission.CALL_PHONE"/>
- </manifest>

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。