android基础--发送通知

 

MainActivity.java

public class MainActivity extends Activity {

       private EditText shortText;

       private EditText titleText;

       private EditText contextText;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //找到三个文本输入框

        shortText = (EditText) this.findViewById(R.id.shorttitle);

        titleText = (EditText) this.findViewById(R.id.title);

        contextText = (EditText) this.findViewById(R.id.context);

    }

    public void send(View view){

           //从三个文本输入框中取得内容

           String shortTitle = shortText.getText().toString();

           String title = titleText.getText().toString();

           String cotext = contextText.getText().toString();

           //构建一个通知

           Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:110"));

           PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

           Notification notification = new Notification(android.R.drawable.stat_notify_chat, shortTitle, System.currentTimeMillis());

           notification.setLatestEventInfo(this, title, cotext, pendingIntent);

           notification.flags = Notification.FLAG_AUTO_CANCEL;//设置通知查看完毕自动关闭

           notification.defaults = Notification.DEFAULT_SOUND;//设置默认声音

           //发送通知

           NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//得到通知管理对象

           manager.notify(1, notification);//发出通知

    }

}

注:加权限

<uses-permission android:name="android.permission.CALL_PHONE"/>

 

阅读更多
上一篇android基础--屏幕切换动画
下一篇android基础--采用方向传感器实现指南针
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭